Advertisement
Lauda

PropertyDialog

Jan 10th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.80 KB | None | 0 0
  1. /***********************************************************************
  2.  * Module:  PropertyDialog
  3.  * Author:  Goran Todorovic
  4.  * Purpose: Dialog used for changing element properties
  5.  ***********************************************************************/
  6. package geer.gui.dialogs;
  7.  
  8. import geer.app.config.Config;
  9. import geer.app.config.sLog;
  10.  
  11. import java.awt.BorderLayout;
  12. import java.awt.Color;
  13. import java.awt.Container;
  14. import java.awt.Dimension;
  15. import java.awt.Font;
  16. import java.lang.reflect.Method;
  17.  
  18. import javax.swing.ComboBoxModel;
  19. import javax.swing.DefaultComboBoxModel;
  20. import javax.swing.JFrame;
  21. import javax.swing.JLabel;
  22. import javax.swing.JPanel;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.SpinnerNumberModel;
  25. import javax.swing.SwingConstants;
  26. import javax.swing.UIManager;
  27. import javax.swing.border.EmptyBorder;
  28. import javax.swing.border.LineBorder;
  29. import javax.swing.event.TableModelEvent;
  30. import javax.swing.event.TableModelListener;
  31. import javax.swing.table.TableCellEditor;
  32.  
  33. import de.javasoft.swing.JYPropertyTable;
  34. import de.javasoft.swing.table.ColorComboBoxTableCellEditor;
  35. import de.javasoft.swing.table.JYComboBoxTableCellEditor;
  36. import de.javasoft.swing.table.PropertyTableModel;
  37. import de.javasoft.swing.table.TableSeparator;
  38.  
  39. /**
  40.  * PropertyDialog class
  41.  * @author Goran Todorovic
  42.  *
  43.  */
  44. public class PropertyDialog extends JFrame {
  45.  
  46.     /**
  47.      * TODO:
  48.      * Implement back-end
  49.      * Fix loading time...sometimes it's slow on opening
  50.      */
  51.    
  52.     /**
  53.      *
  54.      */
  55.     private static final long serialVersionUID = -6633399800771737715L;
  56.  
  57.     /**
  58.      * Test label (can be (or must :P) replacted with an actual model (Entity, Attribute...))
  59.      */
  60.     private JLabel testLabel = null;
  61.  
  62.     public PropertyDialog() {
  63.         super(Config.getInstance().getAppName() + " :: Element properties");
  64.         this.setupPropertyDialog(getContentPane());
  65.  
  66.         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  67.         setSize(400,350);
  68.         setLocationRelativeTo(null);
  69.         setVisible(true);
  70.     }
  71.  
  72.     /**
  73.      * Setup property table values:
  74.      * @param container - content pane
  75.      */
  76.     private void setupPropertyDialog(final Container container)
  77.     {
  78.         JPanel panel = new JPanel();
  79.         panel.setPreferredSize(new Dimension(400, 50));
  80.         panel.setBackground(Color.WHITE);
  81.  
  82.         testLabel = new JLabel("TestLabel");
  83.         testLabel.setPreferredSize(new Dimension(200, 40));
  84.         testLabel.setBorder(new EmptyBorder(10,10,10,10));    
  85.         testLabel.setOpaque(true);
  86.         testLabel.setBackground(new Color(0xBFFFFF));
  87.         testLabel.setForeground(new Color(0x202020));
  88.         testLabel.setBorder(new LineBorder(Color.ORANGE, 2));
  89.         testLabel.setFont(new Font("Dialog", Font.PLAIN, 20));
  90.  
  91.         panel.add(testLabel);
  92.         container.add(panel, BorderLayout.NORTH);
  93.  
  94.         JYPropertyTable table = new JYPropertyTable() {
  95.  
  96.             /**
  97.              *
  98.              */
  99.             private static final long serialVersionUID = 571816901375361004L;
  100.  
  101.             // Override installEditors() to support runtime LAF-switch (can be removed if needed)
  102.             @Override
  103.             protected void installEditors() {
  104.                 super.installEditors();
  105.                 // We use a custom comboBox model to increase usability by readable values (i.e. 2 will be displayed as LEFT)
  106.                 setPropertyEditor(HAlignmentComboModel.class, new HAlignmentCellEditor(defaultEditor));
  107.                 // Replace default color editor with non-editable editor (means combo text field isn't editable)
  108.                 setPropertyEditor(Color.class, new ColorComboBoxTableCellEditor(getDefaultEditor(), false, false, false));
  109.             }
  110.         };
  111.  
  112.         // Add listener to update testLabel properties
  113.         table.getModel().addTableModelListener(new ModelListener());
  114.         initTable(table);
  115.  
  116.         // Workaround to avoid border fallback to default scrollpane-border after a LAF switch (Java 1.5/JTable bug)
  117.         // Can be removed if needed
  118.         JScrollPane scrollPane = new JScrollPane(table) {
  119.             /**
  120.              *
  121.              */
  122.             private static final long serialVersionUID = 7462979487368722909L;
  123.  
  124.             @Override
  125.             public void updateUI() {
  126.                 super.updateUI();
  127.                 setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
  128.             }
  129.         };
  130.         container.add(scrollPane);    
  131.     }
  132.  
  133.     /**
  134.      * Initial table property values.
  135.      * @param table
  136.      */
  137.     private void initTable(JYPropertyTable table)
  138.     {
  139.         table.addProperty("Text", testLabel.getText());
  140.         table.addProperty("Font", testLabel.getFont());
  141.         table.addSeparator();
  142.         table.addProperty("Foreground", testLabel.getForeground());
  143.         table.addProperty("Background", testLabel.getBackground());    
  144.         table.addProperty("Opaque", testLabel.isOpaque());
  145.         LineBorder border = (LineBorder)testLabel.getBorder();
  146.         table.addProperty("BorderColor", border.getLineColor());
  147.         table.addProperty("BorderSize", new SpinnerNumberModel(border.getThickness(), 0, 8, 1));
  148.         table.addSeparator();
  149.         table.addProperty("HorizontalAlignment", new HAlignmentComboModel());
  150.         table.addProperty("PreferredSize", testLabel.getPreferredSize());    
  151.         table.addProperty("Visible", testLabel.isVisible());  
  152.     }
  153.  
  154.     /**
  155.      * Internal class - ModelListener
  156.      * @author Goran Todorovic
  157.      *
  158.      */
  159.     private class ModelListener implements TableModelListener {      
  160.         public void tableChanged(TableModelEvent evt) {
  161.             if (evt.getColumn() == 0)
  162.                 return;
  163.  
  164.             int row = evt.getFirstRow();
  165.             PropertyTableModel model = (PropertyTableModel)evt.getSource();
  166.             Object val = model.getPropertyValue(row);
  167.  
  168.             if (val instanceof TableSeparator)
  169.                 return;
  170.  
  171.             // Convert model alignment value to integer
  172.             else if (val instanceof HAlignmentComboModel) {  
  173.                 HAlignmentComboModel.Alignment align = (HAlignmentComboModel.Alignment)((ComboBoxModel)val).getSelectedItem();
  174.                 val = new Integer(align.toInt());
  175.             }        
  176.             else if (model.getPropertyKey(row).toString().equals("BorderSize")) {
  177.                 int size = (Integer)((SpinnerNumberModel)val).getValue();
  178.                 LineBorder border = (LineBorder)testLabel.getBorder();        
  179.                 testLabel.setBorder(new LineBorder(border.getLineColor(), size));
  180.                 return;
  181.             }
  182.             else if (model.getPropertyKey(row).toString().equals("BorderColor")) {
  183.                 Color color = (Color)val;
  184.                 LineBorder border = (LineBorder)testLabel.getBorder();
  185.                 testLabel.setBorder(new LineBorder(color, border.getThickness()));
  186.                 return;
  187.             }
  188.  
  189.             // Veeeery bugged method below xD
  190.             // Set object value via reflection
  191.             /**
  192.              * What we do here:
  193.              * We take key property from the table above and we update testLabel model value based on that property key
  194.              * IMPORTANT: table property key has to be the same as from the object property method
  195.              * Example: testLabel.setText("bla") - property from here is "Text" because we use "set" + model.getPropertyKey(row) below!
  196.              * otherwise something like this happens: sLog: Error on PropertyDialog.java:184, m = public void javax.swing.JLabel.setText(java.lang.String)
  197.              */
  198.             if (val != null) {
  199.                 Class<?> clazz = val.getClass();
  200.                 if (val instanceof Boolean)
  201.                     clazz = boolean.class;
  202.                 else if (val instanceof Integer)
  203.                     clazz = int.class;
  204.  
  205.                 Method m = null;
  206.                 try {
  207.                     m = testLabel.getClass().getMethod("set" + model.getPropertyKey(row), clazz);
  208.                     sLog.writeConsole("Error on PropertyDialog.java:184, m = " + m.toString());
  209.                     m.invoke(testLabel, val);
  210.                 } catch (Exception e) {
  211.                     sLog.writeConsole("Error on PropertyDialog.java:184, m = " + m.toString());
  212.                     sLog.write(e);
  213.                 }
  214.             }
  215.             testLabel.revalidate();
  216.             testLabel.repaint();
  217.         }
  218.     }
  219.  
  220.     /**
  221.      * Internal Horizontal Alignment TableCellEditor Class
  222.      * @author Goran Todorovic
  223.      * Note: In case we decide not to use aligment for text, everything related to this can be deleted!
  224.      *
  225.      */
  226.     static class HAlignmentCellEditor extends JYComboBoxTableCellEditor {
  227.         /**
  228.          *
  229.          */
  230.         private static final long serialVersionUID = 3463536609804222981L;
  231.  
  232.         public HAlignmentCellEditor(TableCellEditor defaultEditor) {
  233.             super(defaultEditor, false, false);
  234.             editorComponent.setModel(new HAlignmentComboModel());
  235.         }
  236.     }  
  237.  
  238.     /**
  239.      * Internal Horizontal Alignment ComboBox Model Class - has to be public or must implement Cloneable
  240.      * @author Goran Todorovic
  241.      *
  242.      */
  243.     public static class HAlignmentComboModel extends DefaultComboBoxModel {
  244.         /**
  245.          *
  246.          */
  247.         private static final long serialVersionUID = 5341107392291260407L;
  248.  
  249.         public static enum Alignment {
  250.             LEFT(SwingConstants.LEFT),
  251.             CENTER(SwingConstants.CENTER),
  252.             RIGHT(SwingConstants.RIGHT),
  253.             LEADING(SwingConstants.LEADING),
  254.             TRAILING(SwingConstants.TRAILING);
  255.  
  256.             private int alignment;
  257.  
  258.             private Alignment(int alignment) {
  259.                 this.alignment = alignment;
  260.             }
  261.  
  262.             public int toInt() {
  263.                 return alignment;
  264.             }
  265.         }
  266.  
  267.         // Initialize model
  268.         {
  269.             for (Alignment align : Alignment.values())
  270.                 super.addElement(align);
  271.         }
  272.  
  273.         @Override
  274.         public void addElement(Object object) {
  275.         }
  276.  
  277.         @Override
  278.         public void removeElement(Object object) {
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement