1 19 20 25 26 package org.netbeans.modules.css.visual.ui; 27 28 import org.netbeans.modules.css.visual.model.CssProperties; 29 import org.netbeans.modules.css.visual.model.CssStyleData; 30 import java.awt.BorderLayout ; 31 import java.awt.FontMetrics ; 32 import org.netbeans.modules.css.visual.model.CssStyleData; 33 import java.awt.Dimension ; 34 import java.beans.PropertyChangeEvent ; 35 import java.beans.PropertyChangeListener ; 36 import java.awt.Component ; 37 import java.awt.event.*; 38 import javax.swing.table.*; 39 import javax.swing.event.*; 40 import java.util.EventObject ; 41 import javax.swing.tree.*; 42 import java.io.Serializable ; 43 import javax.swing.*; 44 import org.openide.util.NbBundle; 45 46 51 public class MarginStyleEditor extends StyleEditor { 52 MarginDataTable marginDataTable = new MarginDataTable(); 53 54 55 public MarginStyleEditor() { 56 setName("marginStyleEditor"); setDisplayName(NbBundle.getMessage(MarginStyleEditor.class, "MARGIN_EDITOR_DISPNAME")); 58 initComponents(); 59 marginPanel.add(marginDataTable, BorderLayout.CENTER); 60 } 61 62 66 protected void setCssPropertyValues(CssStyleData cssStyleData){ 67 removeCssPropertyChangeListener(); 68 marginDataTable.setCssPropertyValues(cssStyleData); 69 setCssPropertyChangeListener(cssStyleData); 70 marginDataTable.validate(); 71 marginDataTable.repaint(); 72 } 73 74 private void initComponents() { 76 marginPanel = new javax.swing.JPanel (); 77 78 setLayout(new java.awt.BorderLayout (0, 5)); 79 80 marginPanel.setLayout(new java.awt.BorderLayout ()); 81 82 marginPanel.setBorder(new javax.swing.border.CompoundBorder (new javax.swing.border.EmptyBorder (new java.awt.Insets (5, 5, 5, 5)), new javax.swing.border.EtchedBorder ())); 83 add(marginPanel, java.awt.BorderLayout.NORTH); 84 85 } 86 91 public class MarginDataTable extends JTable{ 92 95 private String value; 96 97 MarginWidthField allMarginField; 98 PaddingWidthField allPaddingField; 99 MarginWidthField topMarginField; 100 PaddingWidthField topPaddingField; 101 MarginWidthField bottomMarginField; 102 PaddingWidthField leftPaddingField; 103 MarginWidthField rightMarginField; 104 PaddingWidthField rightPaddingField; 105 PaddingWidthField bottomPaddingField; 106 MarginWidthField leftMarginField; 107 108 public MarginDataTable(){ 109 super(6,3); 110 setDefaultRenderer( JComponent.class, new JComponentCellRenderer() ); 111 setDefaultEditor( JComponent.class, new JComponentCellEditor() ); 112 FontMetrics fontMetrics = getFontMetrics(getFont()); 113 setRowHeight((fontMetrics.getHeight() + 10) > 25 ? (fontMetrics.getHeight() + 10) : 25); 114 setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 115 setIntercellSpacing(new Dimension (5,5)); 116 setTableHeader(null); 118 setBackground(new JPanel().getBackground()); 119 initCells(); 120 getColumnModel().getColumn(0).setPreferredWidth(30); 121 getColumnModel().getColumn(1).setPreferredWidth(150); 122 getColumnModel().getColumn(2).setPreferredWidth(150); 123 getAccessibleContext().setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "MARGIN_STYLE_TABLE_ACCESS_NAME")); 124 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "MARGIN_STYLE_TABLE_ACCESS_DESC")); 125 } 126 127 protected void setCssPropertyValues(CssStyleData cssStyleData){ 128 129 String topMargin = cssStyleData.getProperty(CssProperties.MARGIN_TOP); 131 String bottomMargin = cssStyleData.getProperty(CssProperties.MARGIN_BOTTOM); 132 String leftMargin = cssStyleData.getProperty(CssProperties.MARGIN_LEFT); 133 String rightMargin = cssStyleData.getProperty(CssProperties.MARGIN_RIGHT); 134 135 if ((topMargin != null) && (bottomMargin != null) && (leftMargin != null) && (rightMargin != null) && 136 (topMargin.equals(bottomMargin)) && (topMargin.equals(leftMargin)) && 137 (topMargin.equals(rightMargin))){ 138 allMarginField.setMarginString(topMargin); 139 }else{ 140 allMarginField.setMarginString(null); 141 topMarginField.setMarginString(topMargin); 142 bottomMarginField.setMarginString(bottomMargin); 143 leftMarginField.setMarginString(leftMargin); 144 rightMarginField.setMarginString(rightMargin); 145 } 146 147 String topPadding = cssStyleData.getProperty(CssProperties.PADDING_TOP); 149 String bottomPadding = cssStyleData.getProperty(CssProperties.PADDING_BOTTOM); 150 String leftPadding = cssStyleData.getProperty(CssProperties.PADDING_LEFT); 151 String rightPadding = cssStyleData.getProperty(CssProperties.PADDING_RIGHT); 152 153 if ((topPadding != null) && (bottomPadding != null) && (leftPadding != null) && (rightPadding != null) && 154 (topPadding.equals(bottomPadding)) && (topPadding.equals(leftPadding)) && 155 (topPadding.equals(rightPadding))){ 156 allPaddingField.setPaddingString(topPadding); 157 }else{ 158 allPaddingField.setPaddingString(null); 159 topPaddingField.setPaddingString(topPadding); 160 bottomPaddingField.setPaddingString(bottomPadding); 161 leftPaddingField.setPaddingString(leftPadding); 162 rightPaddingField.setPaddingString(rightPadding); 163 } 164 165 } 166 167 public void initCells(){ 168 JPanel colHeader1 = new JPanel(); 169 setValueAt(colHeader1, 0, 0 ); 171 JPanel colHeader2 = new JPanel(); 172 colHeader2.add(new JLabel(org.openide.util.NbBundle.getMessage(MarginDataTable.class, "MARGIN_ALL"))); 173 setValueAt(colHeader2, 0, 1 ); 174 JPanel colHeader3 = new JPanel(); 175 colHeader3.add(new JLabel(org.openide.util.NbBundle.getMessage(MarginDataTable.class, "PADDING"))); 176 setValueAt(colHeader3, 0, 2 ); 177 178 JPanel rowHeader1 = new JPanel(); 179 rowHeader1.add(new JLabel(org.openide.util.NbBundle.getMessage(MarginDataTable.class, "MARGIN_PADDING_ALL"))); 180 setValueAt(rowHeader1, 1, 0 ); 181 JPanel rowHeader2 = new JPanel(); 182 rowHeader2.add(new JLabel(org.openide.util.NbBundle.getMessage(MarginDataTable.class, "MARGIN_PADDING_TOP"))); 183 setValueAt(rowHeader2, 2, 0 ); 184 JPanel rowHeader3 = new JPanel(); 185 rowHeader3.add(new JLabel(org.openide.util.NbBundle.getMessage(MarginDataTable.class, "MARGIN_PADDING_BOTTOM"))); 186 setValueAt(rowHeader3, 3, 0 ); 187 JPanel rowHeader4 = new JPanel(); 188 rowHeader4.add(new JLabel(org.openide.util.NbBundle.getMessage(MarginDataTable.class, "MARGIN_PADDING_LEFT"))); 189 setValueAt(rowHeader4, 4, 0 ); 190 JPanel rowHeader5 = new JPanel(); 191 rowHeader5.add(new JLabel(org.openide.util.NbBundle.getMessage(MarginDataTable.class, "MARGIN_PADDING_RIGHT"))); 192 setValueAt(rowHeader5, 5, 0 ); 193 194 allMarginField = new MarginWidthField(); 196 allMarginField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "ALL_SIDE_MARGIN_WIDTH_ACCESS_NAME"), 197 NbBundle.getMessage(MarginDataTable.class, "ALL_SIDE_MARGIN_WIDTH_UNIT_ACCESS_NAME")); 198 allMarginField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "ALL_SIDE_MARGIN_WIDTH_ACCESS_DESC"), 199 NbBundle.getMessage(MarginDataTable.class, "ALL_SIDE_MARGIN_WIDTH_UNIT_ACCESS_DESC")); 200 allMarginField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 201 public void propertyChange(PropertyChangeEvent evt) { 202 topMarginField.setMarginString(evt.getNewValue().toString()); 204 bottomMarginField.setMarginString(evt.getNewValue().toString()); 205 leftMarginField.setMarginString(evt.getNewValue().toString()); 206 rightMarginField.setMarginString(evt.getNewValue().toString()); 207 } 208 }); 209 setValueAt(allMarginField, 1, 1); 210 211 212 213 allPaddingField = new PaddingWidthField(); 215 allPaddingField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "ALL_SIDE_PADDING_WIDTH_ACCESS_NAME"), 216 NbBundle.getMessage(MarginDataTable.class, "ALL_SIDE_PADDING_WIDTH_UNIT_ACCESS_NAME")); 217 allPaddingField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "ALL_SIDE_PADDING_WIDTH_ACCESS_DESC"), 218 NbBundle.getMessage(MarginDataTable.class, "ALL_SIDE_PADDING_WIDTH_UNIT_ACCESS_DESC")); 219 allPaddingField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 220 public void propertyChange(PropertyChangeEvent evt) { 221 topPaddingField.setPaddingString(evt.getNewValue().toString()); 223 bottomPaddingField.setPaddingString(evt.getNewValue().toString()); 224 leftPaddingField.setPaddingString(evt.getNewValue().toString()); 225 rightPaddingField.setPaddingString(evt.getNewValue().toString()); 226 } 227 }); 228 setValueAt(allPaddingField, 1, 2); 229 230 231 topMarginField = new MarginWidthField(); 233 topMarginField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "TOP_SIDE_MARGIN_WIDTH_ACCESS_NAME"), 234 NbBundle.getMessage(MarginDataTable.class, "TOP_SIDE_MARGIN_WIDTH_UNIT_ACCESS_NAME")); 235 topMarginField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "TOP_SIDE_MARGIN_WIDTH_ACCESS_DESC"), 236 NbBundle.getMessage(MarginDataTable.class, "TOP_SIDE_MARGIN_WIDTH_UNIT_ACCESS_DESC")); 237 topMarginField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 238 public void propertyChange(PropertyChangeEvent evt) { 239 cssPropertyChangeSupport.firePropertyChange(CssProperties.MARGIN_TOP, null, evt.getNewValue().toString()); 240 } 241 }); 242 setValueAt(topMarginField, 2, 1); 243 244 topPaddingField = new PaddingWidthField(); 246 topPaddingField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "TOP_SIDE_PADDING_WIDTH_ACCESS_NAME"), 247 NbBundle.getMessage(MarginDataTable.class, "TOP_SIDE_PADDING_WIDTH_UNIT_ACCESS_NAME")); 248 topPaddingField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "TOP_SIDE_PADDING_WIDTH_ACCESS_DESC"), 249 NbBundle.getMessage(MarginDataTable.class, "TOP_SIDE_PADDING_WIDTH_UNIT_ACCESS_DESC")); 250 topPaddingField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 251 public void propertyChange(PropertyChangeEvent evt) { 252 cssPropertyChangeSupport.firePropertyChange(CssProperties.PADDING_TOP, null, evt.getNewValue().toString()); 253 } 254 }); 255 setValueAt(topPaddingField, 2, 2); 256 257 bottomMarginField = new MarginWidthField(); 259 bottomMarginField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "BOTTOM_SIDE_MARGIN_WIDTH_ACCESS_NAME"), 260 NbBundle.getMessage(MarginDataTable.class, "BOTTOM_SIDE_MARGIN_WIDTH_UNIT_ACCESS_NAME")); 261 bottomMarginField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "BOTTOM_SIDE_MARGIN_WIDTH_ACCESS_DESC"), 262 NbBundle.getMessage(MarginDataTable.class, "BOTTOM_SIDE_MARGIN_WIDTH_UNIT_ACCESS_DESC")); 263 bottomMarginField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 264 public void propertyChange(PropertyChangeEvent evt) { 265 cssPropertyChangeSupport.firePropertyChange(CssProperties.MARGIN_BOTTOM, null, evt.getNewValue().toString()); 266 } 267 }); 268 setValueAt(bottomMarginField, 3, 1); 269 270 bottomPaddingField = new PaddingWidthField(); 272 bottomPaddingField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "BOTTOM_SIDE_PADDING_WIDTH_ACCESS_NAME"), 273 NbBundle.getMessage(MarginDataTable.class, "BOTTOM_SIDE_PADDING_WIDTH_UNIT_ACCESS_NAME")); 274 bottomPaddingField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "BOTTOM_SIDE_PADDING_WIDTH_ACCESS_DESC"), 275 NbBundle.getMessage(MarginDataTable.class, "BOTTOM_SIDE_PADDING_WIDTH_UNIT_ACCESS_DESC")); 276 bottomPaddingField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 277 public void propertyChange(PropertyChangeEvent evt) { 278 cssPropertyChangeSupport.firePropertyChange(CssProperties.PADDING_BOTTOM, null, evt.getNewValue().toString()); 279 } 280 }); 281 setValueAt(bottomPaddingField, 3, 2); 282 283 leftMarginField = new MarginWidthField(); 285 leftMarginField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "LEFT_SIDE_MARGIN_WIDTH_ACCESS_NAME"), 286 NbBundle.getMessage(MarginDataTable.class, "LEFT_SIDE_MARGIN_WIDTH_UNIT_ACCESS_NAME")); 287 leftMarginField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "LEFT_SIDE_MARGIN_WIDTH_ACCESS_DESC"), 288 NbBundle.getMessage(MarginDataTable.class, "LEFT_SIDE_MARGIN_WIDTH_UNIT_ACCESS_DESC")); 289 leftMarginField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 290 public void propertyChange(PropertyChangeEvent evt) { 291 cssPropertyChangeSupport.firePropertyChange(CssProperties.MARGIN_LEFT, null, evt.getNewValue().toString()); 292 } 293 }); 294 setValueAt(leftMarginField, 4, 1); 295 296 leftPaddingField = new PaddingWidthField(); 298 leftPaddingField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "LEFT_SIDE_PADDING_WIDTH_ACCESS_NAME"), 299 NbBundle.getMessage(MarginDataTable.class, "LEFT_SIDE_PADDING_WIDTH_UNIT_ACCESS_NAME")); 300 leftPaddingField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "LEFT_SIDE_PADDING_WIDTH_ACCESS_DESC"), 301 NbBundle.getMessage(MarginDataTable.class, "LEFT_SIDE_PADDING_WIDTH_UNIT_ACCESS_DESC")); 302 leftPaddingField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 303 public void propertyChange(PropertyChangeEvent evt) { 304 cssPropertyChangeSupport.firePropertyChange(CssProperties.PADDING_LEFT, null, evt.getNewValue().toString()); 305 } 306 }); 307 setValueAt(leftPaddingField, 4, 2); 308 309 rightMarginField = new MarginWidthField(); 311 rightMarginField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "RIGHT_SIDE_MARGIN_WIDTH_ACCESS_NAME"), 312 NbBundle.getMessage(MarginDataTable.class, "RIGHT_SIDE_MARGIN_WIDTH_UNIT_ACCESS_NAME")); 313 rightMarginField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "RIGHT_SIDE_MARGIN_WIDTH_ACCESS_DESC"), 314 NbBundle.getMessage(MarginDataTable.class, "RIGHT_SIDE_MARGIN_WIDTH_UNIT_ACCESS_DESC")); 315 rightMarginField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 316 public void propertyChange(PropertyChangeEvent evt) { 317 cssPropertyChangeSupport.firePropertyChange(CssProperties.MARGIN_RIGHT, null, evt.getNewValue().toString()); 318 } 319 }); 320 setValueAt(rightMarginField, 5, 1); 321 322 rightPaddingField = new PaddingWidthField(); 324 rightPaddingField.setAccessibleName(NbBundle.getMessage(MarginDataTable.class, "RIGHT_SIDE_PADDING_WIDTH_ACCESS_NAME"), 325 NbBundle.getMessage(MarginDataTable.class, "RIGHT_SIDE_PADDING_WIDTH_UNIT_ACCESS_NAME")); 326 rightPaddingField.setAccessibleDescription(NbBundle.getMessage(MarginDataTable.class, "RIGHT_SIDE_PADDING_WIDTH_ACCESS_DESC"), 327 NbBundle.getMessage(MarginDataTable.class, "RIGHT_SIDE_PADDING_WIDTH_UNIT_ACCESS_DESC")); 328 rightPaddingField.addPropertyChangeListener(new PropertyChangeListenerImpl() { 329 public void propertyChange(PropertyChangeEvent evt) { 330 cssPropertyChangeSupport.firePropertyChange(CssProperties.PADDING_RIGHT, null, evt.getNewValue().toString()); 331 } 332 }); 333 setValueAt(rightPaddingField, 5, 2); 334 } 335 336 class PropertyChangeListenerImpl implements PropertyChangeListener { 337 public void propertyChange(PropertyChangeEvent evt) { 338 } 339 } 340 341 public TableCellRenderer getCellRenderer(int row, int column) { 342 TableColumn tableColumn = getColumnModel().getColumn(column); 343 TableCellRenderer renderer = tableColumn.getCellRenderer(); 344 if (renderer == null) { 345 Class c = getColumnClass(column); 346 if( c.equals(Object .class) ) { 347 Object o = getValueAt(row,column); 348 if( o != null ) 349 c = getValueAt(row,column).getClass(); 350 } 351 renderer = getDefaultRenderer(c); 352 } 353 return renderer; 354 } 355 356 public TableCellEditor getCellEditor(int row, int column) { 357 TableColumn tableColumn = getColumnModel().getColumn(column); 358 TableCellEditor editor = tableColumn.getCellEditor(); 359 if (editor == null) { 360 Class c = getColumnClass(column); 361 if( c.equals(Object .class) ) { 362 Object o = getValueAt(row,column); 363 if( o != null ) 364 c = getValueAt(row,column).getClass(); 365 } 366 editor = getDefaultEditor(c); 367 } 368 return editor; 369 } 370 371 private class JComponentCellRenderer implements TableCellRenderer { 372 public Component getTableCellRendererComponent(JTable table, Object value, 373 boolean isSelected, boolean hasFocus, int row, int column) { 374 JComponent comp = (JComponent)value; 375 comp.setMinimumSize(new Dimension (100,25)); 376 return comp; 377 } 378 } 379 380 private class JComponentCellEditor implements TableCellEditor, TreeCellEditor, 381 Serializable { 382 383 protected EventListenerList listenerList = new EventListenerList(); 384 transient protected ChangeEvent changeEvent = null; 385 386 protected JComponent editorComponent = null; 387 protected JComponent container = null; 389 390 public Component getComponent() { 391 return editorComponent; 392 } 393 394 395 public Object getCellEditorValue() { 396 return editorComponent; 397 } 398 399 public boolean isCellEditable(EventObject anEvent) { 400 return true; 401 } 402 403 public boolean shouldSelectCell(EventObject anEvent) { 404 if( editorComponent != null && anEvent instanceof MouseEvent 405 && ((MouseEvent)anEvent).getID() == MouseEvent.MOUSE_PRESSED ) { 406 Component dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent, 3, 3 ); 407 MouseEvent e = (MouseEvent)anEvent; 408 MouseEvent e2 = new MouseEvent( dispatchComponent, MouseEvent.MOUSE_RELEASED, 409 e.getWhen() + 100000, e.getModifiers(), 3, 3, e.getClickCount(), 410 e.isPopupTrigger() ); 411 dispatchComponent.dispatchEvent(e2); 412 e2 = new MouseEvent( dispatchComponent, MouseEvent.MOUSE_CLICKED, 413 e.getWhen() + 100001, e.getModifiers(), 3, 3, 1, 414 e.isPopupTrigger() ); 415 dispatchComponent.dispatchEvent(e2); 416 } 417 return false; 418 } 419 420 public boolean stopCellEditing() { 421 fireEditingStopped(); 422 return true; 423 } 424 425 public void cancelCellEditing() { 426 fireEditingCanceled(); 427 } 428 429 public void addCellEditorListener(CellEditorListener l) { 430 listenerList.add(CellEditorListener.class, l); 431 } 432 433 public void removeCellEditorListener(CellEditorListener l) { 434 listenerList.remove(CellEditorListener.class, l); 435 } 436 437 protected void fireEditingStopped() { 438 Object [] listeners = listenerList.getListenerList(); 439 for (int i = listeners.length-2; i>=0; i-=2) { 442 if (listeners[i]==CellEditorListener.class) { 443 if (changeEvent == null) 445 changeEvent = new ChangeEvent (this); 446 ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent); 447 } 448 } 449 } 450 451 protected void fireEditingCanceled() { 452 Object [] listeners = listenerList.getListenerList(); 454 for (int i = listeners.length-2; i>=0; i-=2) { 457 if (listeners[i]==CellEditorListener.class) { 458 if (changeEvent == null) 460 changeEvent = new ChangeEvent (this); 461 ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent); 462 } 463 } 464 } 465 466 public Component getTreeCellEditorComponent(JTree tree, Object value, 468 boolean isSelected, boolean expanded, boolean leaf, int row) { 469 String stringValue = tree.convertValueToText(value, isSelected, 470 expanded, leaf, row, false); 471 472 editorComponent = (JComponent)value; 473 container = tree; 474 return editorComponent; 475 } 476 477 public Component getTableCellEditorComponent(JTable table, Object value, 479 boolean isSelected, int row, int column) { 480 481 editorComponent = (JComponent)value; 482 container = table; 483 return editorComponent; 484 } 485 } } 487 488 private javax.swing.JPanel marginPanel; 490 492 } 493 | Popular Tags |