Revision: 3775
Author: [email protected]
Date: Thu Jul 22 09:06:16 2010
Log: Added the ability to add arbitrary settings to the critic settings. The critic settings panel can also display these settings and accept user changes.

Currently only enums for settings are supported with a combo box but there is a place for boolean and string settings for the future.
http://code.google.com/p/power-architect/source/detail?r=3775

Modified:
/trunk/src/main/java/ca/sqlpower/architect/ddl/critic/CriticAndSettings.java /trunk/src/main/java/ca/sqlpower/architect/swingui/critic/CriticSettingsPanel.java

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/ddl/critic/CriticAndSettings.java Fri Jul 16 07:18:35 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/ddl/critic/CriticAndSettings.java Thu Jul 22 09:06:16 2010
@@ -21,6 +21,7 @@

 import java.util.Collections;
 import java.util.List;
+import java.util.Map;

 import ca.sqlpower.architect.ArchitectProject;
 import ca.sqlpower.architect.ddl.DB2DDLGenerator;
@@ -40,6 +41,7 @@
 import ca.sqlpower.object.annotation.Constructor;
 import ca.sqlpower.object.annotation.ConstructorParameter;
 import ca.sqlpower.object.annotation.Mutator;
+import ca.sqlpower.object.annotation.NonProperty;

 /**
* The settings of a specific {...@link Critic}. Includes if the critic is enabled
@@ -262,4 +264,50 @@
ArchitectSwingSession session = (ArchitectSwingSession) project.getSession();
         return session;
     }
-}
+
+    /**
+     * Returns a map of property names to their properties that a user can
+ * change and update to affect the settings of a critic. These properties + * can be of different types which can affect how the user can interact with
+     * them. This method exists to allow an editor interface to display and
+ * change the properties of a critic in a way that is not implementation
+     * specific.
+     * <p>
+     * If this method is overridden you must also override
+     * {...@link #setProperty(String, Object)}
+     * <p>
+     * If the properties in this map are to be persisted they need proper
+     * getters and setters that are annotated.
+     */
+    @NonProperty
+    public Map<String, Object> getProperties() {
+        return Collections.emptyMap();
+    }
+
+    /**
+     * @see #getProperties()
+     */
+    @NonProperty
+    public void setProperty(String propertyName, Object value) {
+        //do nothing
+    }
+
+    /**
+ * Returns a user-friendly description of what the property is used for in the + * critic. If the property name is not known in this critic a null value will be returned.
+     */
+    @NonProperty
+    public String getPropertyDescription(String propertyName) {
+        return null;
+    }
+
+    /**
+ * Returns the list of enum objects that the given property can be set to. + * If the property is not an enum or does not exist in the object null will
+     * be returned.
+     */
+    public List<Object> getEnumPropertyValues(String propertyName) {
+        return null;
+    }
+
+}
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/swingui/critic/CriticSettingsPanel.java Tue Jul 13 15:38:56 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/swingui/critic/CriticSettingsPanel.java Thu Jul 22 09:06:16 2010
@@ -22,10 +22,13 @@
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;

 import javax.swing.JComboBox;
 import javax.swing.JComponent;
+import javax.swing.JLabel;
 import javax.swing.JPanel;

 import ca.sqlpower.architect.ddl.critic.CriticAndSettings;
@@ -65,6 +68,12 @@
      * on these changes can listen and be notified here.
      */
private final List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
+
+    /**
+ * This map stores all of the property names to their combo boxes that a
+     * user can use to change the property of a setting with.
+     */
+ private final Map<String, JComboBox> criticPropertyChoosers = new HashMap<String, JComboBox>();

     /**
      * Updates the severity combo box due to changes to the model.
@@ -85,6 +94,18 @@
     public CriticSettingsPanel(CriticAndSettings settings) {
         this.settings = settings;

+ for (Map.Entry<String, Object> entry : settings.getProperties().entrySet()) {
+            if (entry.getValue() instanceof Boolean) {
+ //TODO display a check box with the parameter name to the left of it
+            } else if (entry.getValue() instanceof String) {
+ //TODO display a text field with a parameter name to the left of it
+            } else if (entry.getValue().getClass().isEnum()) {
+ JComboBox box = new JComboBox(settings.getEnumPropertyValues(entry.getKey()).toArray());
+                box.setSelectedItem(entry.getValue());
+                criticPropertyChoosers.put(entry.getKey(), box);
+            }
+        }
+
         panel = new JPanel();
         severityCombo = new JComboBox(Severity.values());
if (settings.getPlatformType().equals(StarterPlatformTypes.CONFIGURATION.getName())) {
@@ -93,15 +114,26 @@
         severityCombo.setSelectedItem(settings.getSeverity());
//It would be nice if the layout used a pref:grow style for the first //column but it makes it difficult to set the preferred size correctly. - DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout("fill:pref:grow, 5dlu, pref"), panel);
-        builder.append(settings.getName());
+ DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout("pref, 10dlu, fill:pref:grow, 5dlu, pref"), panel);
+        builder.append(new JLabel(settings.getName()), 3);
         builder.append(severityCombo);

+ for (Map.Entry<String, JComboBox> entry : criticPropertyChoosers.entrySet()) {
+            builder.nextLine();
+            builder.append("");
+ builder.append(settings.getPropertyDescription(entry.getKey()));
+            builder.append(entry.getValue());
+
+        }
+
         settings.addSPListener(severitySettingListener);
     }

     public boolean applyChanges() {
         settings.setSeverity(((Severity) severityCombo.getSelectedItem()));
+ for (Map.Entry<String, JComboBox> enumProperty : criticPropertyChoosers.entrySet()) { + settings.setProperty(enumProperty.getKey(), enumProperty.getValue().getSelectedItem());
+        }
         return true;
     }

@@ -115,6 +147,12 @@

     public boolean hasUnsavedChanges() {
if (!((Severity) severityCombo.getSelectedItem()).equals(settings.getSeverity())) return true; + for (Map.Entry<String, JComboBox> enumProperty : criticPropertyChoosers.entrySet()) { + if ((settings.getProperties().get(enumProperty.getKey()) == null && enumProperty.getValue().getSelectedItem() != null) + | | !settings.getProperties().get(enumProperty.getKey()).equals(enumProperty.getValue().getSelectedItem())) {
+                return true;
+            }
+        }
         return false;
     }

Reply via email to