Author: jfuerth
Date: Wed May 6 08:38:02 2009
New Revision: 3042
Modified:
trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateLevelAction.java
trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateOLAPChildAction.java
Log:
Fixed bug 1809, which required a small refactoring so the subclasses of
CreateOLAPChildAction can have their own logic to determine the tooltip
text.
Modified:
trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateLevelAction.java
==============================================================================
---
trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateLevelAction.java
(original)
+++
trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateLevelAction.java
Wed May 6 08:38:02 2009
@@ -21,12 +21,15 @@
import java.util.List;
+import javax.swing.KeyStroke;
+
import ca.sqlpower.architect.olap.OLAPUtil;
import ca.sqlpower.architect.olap.MondrianModel.Dimension;
import ca.sqlpower.architect.olap.MondrianModel.Hierarchy;
import ca.sqlpower.architect.olap.MondrianModel.Level;
import ca.sqlpower.architect.swingui.ArchitectSwingSession;
import ca.sqlpower.architect.swingui.PlayPen;
+import ca.sqlpower.architect.swingui.PlayPenComponent;
import ca.sqlpower.architect.swingui.olap.DimensionPane;
import ca.sqlpower.architect.swingui.olap.LevelEditPanel;
import ca.sqlpower.architect.swingui.olap.OSUtils;
@@ -40,6 +43,19 @@
super(session, olapPlayPen, "Level",
DimensionPane.class, "Hierarchy", 'l', OSUtils.LEVEL_ADD_ICON);
}
+ /**
+ * Describes the location where the new level should be added.
+ */
+ private static class AddLocation {
+ private final Hierarchy newParent;
+ private final int newIndex;
+
+ public AddLocation(Hierarchy newParent, int newIndex) {
+ this.newParent = newParent;
+ this.newIndex = newIndex;
+ }
+ }
+
@Override
protected Level addNewChild(DimensionPane pane) {
// first, we have to find or make the parent hierarchy
@@ -50,9 +66,23 @@
d.addHierarchy(h);
}
+ AddLocation addLocation = chooseAddLocation(pane);
+
+ Level l = new Level();
+ int count = 1;
+ while (!OLAPUtil.isNameUnique(addLocation.newParent,
Level.class, "New Level " + count)) {
+ count++;
+ }
+ l.setName("New Level " + count);
+
+ addLocation.newParent.addLevel(addLocation.newIndex, l);
+ return l;
+ }
+
+ private AddLocation chooseAddLocation(DimensionPane pane) {
Hierarchy newParent;
int newIndex;
-
+
// If there are levels selected, we'll add after the last one
List<Level> levels = pane.getSelectedLevels();
if (!levels.isEmpty()) {
@@ -63,6 +93,7 @@
} else {
// no levels were selected, so we'll add to the end of the
selected section
// (or the first section if nothing was selected at all)
+ Dimension d = pane.getModel();
newParent = d.getHierarchies().get(0);
for (int i = 0; i < d.getHierarchies().size(); i++) {
if (pane.isSectionSelected(pane.getSections().get(i))) {
@@ -72,17 +103,8 @@
}
newIndex = newParent.getLevels().size();
}
-
- Level l = new Level();
- int count = 1;
- while (!OLAPUtil.isNameUnique(newParent, Level.class, "New Level "
+ count)) {
- count++;
- }
- l.setName("New Level " + count);
-
- newParent.addLevel(newIndex, l);
- return l;
+ return new AddLocation(newParent, newIndex);
}
@Override
@@ -94,4 +116,20 @@
}
}
+ @Override
+ protected void updateActionState() {
+ List<PlayPenComponent> selectedItems = playpen.getSelectedItems();
+ String description;
+ if (selectedItems.size() == 1 && selectedItems.get(0) instanceof
DimensionPane) {
+ setEnabled(true);
+ DimensionPane pane = (DimensionPane) selectedItems.get(0);
+ AddLocation addLocation = chooseAddLocation(pane);
+ description = "Add Level to " +
addLocation.newParent.getName();
+ } else {
+ setEnabled(false);
+ description = "Add Level to selected Hierarchy" +
+ " (" + ((KeyStroke) getValue(ACCELERATOR_KEY)).getKeyChar()
+ ")";
+ }
+ putValue(SHORT_DESCRIPTION, description);
+ }
}
Modified:
trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateOLAPChildAction.java
==============================================================================
---
trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateOLAPChildAction.java
(original)
+++
trunk/src/ca/sqlpower/architect/swingui/olap/action/CreateOLAPChildAction.java
Wed May 6 08:38:02 2009
@@ -56,29 +56,12 @@
*/
private class PlayPenWatcher implements SelectionListener {
- public PlayPenWatcher() {
- }
-
- private void updateStatus() {
- List<PlayPenComponent> selectedItems =
playpen.getSelectedItems();
- String description;
- if (selectedItems.size() == 1 &&
paneClass.isInstance(selectedItems.get(0))) {
- setEnabled(true);
- description = "Add " + friendlyChildName + " to " +
selectedItems.get(0).getName();
- } else {
- setEnabled(false);
- description = "Add " + friendlyChildName + " to selected "
+ friendlyParentName +
- " (" + ((KeyStroke)
getValue(ACCELERATOR_KEY)).getKeyChar() + ")";
- }
- putValue(SHORT_DESCRIPTION, description);
- }
-
public void itemDeselected(SelectionEvent e) {
- updateStatus();
+ updateActionState();
}
public void itemSelected(SelectionEvent e) {
- updateStatus();
+ updateActionState();
}
}
@@ -118,7 +101,7 @@
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelKey));
PlayPenWatcher ppw = new PlayPenWatcher();
playpen.addSelectionListener(ppw);
- ppw.updateStatus();
+ updateActionState();
}
public void actionPerformed(ActionEvent e) {
@@ -172,4 +155,28 @@
* @param model the item that should be edited in the data entry panel
*/
protected abstract DataEntryPanel createDataEntryPanel(C model);
+
+ /**
+ * This method is called whenever the selection changes in the OLAP
playpen.
+ * If you override this method, you should call {...@link
#setEnabled(boolean)}
+ * and {...@link #putValue(String, Object)} with a key of
SHORT_DESCRIPTION in
+ * order to update the tooltip for this action.
+ * <p>
+ * Note that the default implementation is normally acceptable; there
are only
+ * a few cases (such as levels, which get added under hierarchies in
dimension
+ * panes) where overriding is necessary.
+ */
+ protected void updateActionState() {
+ List<PlayPenComponent> selectedItems = playpen.getSelectedItems();
+ String description;
+ if (selectedItems.size() == 1 &&
paneClass.isInstance(selectedItems.get(0))) {
+ setEnabled(true);
+ description = "Add " + friendlyChildName + " to " +
selectedItems.get(0).getName();
+ } else {
+ setEnabled(false);
+ description = "Add " + friendlyChildName + " to selected " +
friendlyParentName +
+ " (" + ((KeyStroke) getValue(ACCELERATOR_KEY)).getKeyChar()
+ ")";
+ }
+ putValue(SHORT_DESCRIPTION, description);
+ }
}