From 99205455e7a68f4443769176e7f2d6a68014884d Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Wed, 26 Mar 2008 03:23:40 +0000 Subject: [PATCH] Move the common functionality from ObjectEditorDialog and PortalDialog into EditorDialog git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@578 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../stage/tools/editor/EditorDialog.java | 75 ++++++++++ .../stage/tools/editor/EditorFrame.java | 9 +- .../stage/tools/editor/EditorScenePanel.java | 2 +- .../tools/editor/ObjectEditorDialog.java | 129 +++++++----------- .../stage/tools/editor/PortalDialog.java | 69 ++++------ 5 files changed, 152 insertions(+), 132 deletions(-) create mode 100644 src/java/com/threerings/stage/tools/editor/EditorDialog.java diff --git a/src/java/com/threerings/stage/tools/editor/EditorDialog.java b/src/java/com/threerings/stage/tools/editor/EditorDialog.java new file mode 100644 index 00000000..e0fa5cef --- /dev/null +++ b/src/java/com/threerings/stage/tools/editor/EditorDialog.java @@ -0,0 +1,75 @@ +package com.threerings.stage.tools.editor; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.BorderFactory; +import javax.swing.JComponent; +import javax.swing.JInternalFrame; +import javax.swing.JPanel; + +import com.samskivert.swing.HGroupLayout; +import com.samskivert.swing.VGroupLayout; +import com.threerings.stage.tools.editor.util.EditorContext; +import com.threerings.stage.tools.editor.util.EditorDialogUtil; + +/** + * Basic ok cancel dialog for use by editor components. + */ +public abstract class EditorDialog extends JInternalFrame + implements ActionListener +{ + public EditorDialog (String title, EditorContext ctx, EditorScenePanel panel) + { + super(title, true); + _ctx = ctx; + _panel = panel; + // get a handle on the top-level panel + JPanel top = (JPanel)getContentPane(); + + // set up a layout manager for the panel + VGroupLayout gl = new VGroupLayout(VGroupLayout.STRETCH, VGroupLayout.STRETCH, 5, + VGroupLayout.CENTER); + top.setLayout(gl); + top.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); + addComponents(top); + // create our OK/Cancel buttons + JPanel sub = HGroupLayout.makeButtonBox(HGroupLayout.CENTER); + EditorDialogUtil.addButton(this, sub, "OK", "ok"); + EditorDialogUtil.addButton(this, sub, "Cancel", "cancel"); + top.add(sub); + pack(); + } + + public abstract void addComponents (JComponent dialogBody); + + public void actionPerformed (ActionEvent e) + { + String cmd = e.getActionCommand(); + + if (cmd.equals("ok")) { + accepted(); + } else if (cmd.equals("cancel")) { + cancelled(); + } else { + System.err.println("Received unknown action: " + e); + return; + } + + // hide the dialog + EditorDialogUtil.dismiss(this); + + } + + /** + * Called when ok is clicked. + */ + public abstract void accepted (); + + /** Called when cancel is clicked. */ + public void cancelled () + {} + + protected EditorContext _ctx; + protected EditorScenePanel _panel; +} \ No newline at end of file diff --git a/src/java/com/threerings/stage/tools/editor/EditorFrame.java b/src/java/com/threerings/stage/tools/editor/EditorFrame.java index f4462a19..4b8a8943 100644 --- a/src/java/com/threerings/stage/tools/editor/EditorFrame.java +++ b/src/java/com/threerings/stage/tools/editor/EditorFrame.java @@ -23,7 +23,6 @@ package com.threerings.stage.tools.editor; import java.awt.BorderLayout; import java.awt.EventQueue; - import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; @@ -31,7 +30,6 @@ import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; - import java.io.File; import javax.swing.JFileChooser; @@ -48,19 +46,16 @@ import com.samskivert.swing.GroupLayout; import com.samskivert.swing.HGroupLayout; import com.samskivert.swing.util.MenuUtil; import com.samskivert.swing.util.SwingUtil; - import com.threerings.media.ManagedJFrame; - import com.threerings.miso.tile.BaseTileSet; import com.threerings.miso.util.MisoSceneMetrics; - import com.threerings.stage.data.StageScene; import com.threerings.stage.data.StageSceneModel; - import com.threerings.stage.tools.editor.util.EditorContext; import com.threerings.stage.tools.editor.util.EditorDialogUtil; import com.threerings.stage.tools.xml.StageSceneParser; import com.threerings.stage.tools.xml.StageSceneWriter; +import com.threerings.whirled.tools.xml.SceneParser; public class EditorFrame extends ManagedJFrame { @@ -580,7 +575,7 @@ public class EditorFrame extends ManagedJFrame protected EditorContext _ctx; /** We use this to load scenes. */ - protected StageSceneParser _parser = new StageSceneParser(); + protected SceneParser _parser = new StageSceneParser(); /** We use this to save scenes. */ protected StageSceneWriter _writer; diff --git a/src/java/com/threerings/stage/tools/editor/EditorScenePanel.java b/src/java/com/threerings/stage/tools/editor/EditorScenePanel.java index 7c60a695..6b6abe4f 100644 --- a/src/java/com/threerings/stage/tools/editor/EditorScenePanel.java +++ b/src/java/com/threerings/stage/tools/editor/EditorScenePanel.java @@ -335,7 +335,7 @@ public class EditorScenePanel extends StageScenePanel { // create our portal dialog if we haven't yet if (_dialogPortal == null) { - _dialogPortal = new PortalDialog(); + _dialogPortal = new PortalDialog(_ctx, this); } // pass location information on to the dialog diff --git a/src/java/com/threerings/stage/tools/editor/ObjectEditorDialog.java b/src/java/com/threerings/stage/tools/editor/ObjectEditorDialog.java index 3cc9fa7e..4aa4a24d 100644 --- a/src/java/com/threerings/stage/tools/editor/ObjectEditorDialog.java +++ b/src/java/com/threerings/stage/tools/editor/ObjectEditorDialog.java @@ -12,7 +12,7 @@ // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public @@ -21,20 +21,16 @@ package com.threerings.stage.tools.editor; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.util.Arrays; -import javax.swing.BorderFactory; import javax.swing.JComboBox; -import javax.swing.JInternalFrame; +import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.JTextField; import com.samskivert.swing.HGroupLayout; -import com.samskivert.swing.VGroupLayout; import com.samskivert.util.StringUtil; import com.threerings.media.image.ColorPository.ColorRecord; @@ -48,36 +44,27 @@ import com.threerings.media.tile.TileUtil; import com.threerings.miso.client.SceneObject; import com.threerings.stage.tools.editor.util.EditorContext; -import com.threerings.stage.tools.editor.util.EditorDialogUtil; /** * Used to edit object attributes. */ -public class ObjectEditorDialog extends JInternalFrame - implements ActionListener +public class ObjectEditorDialog extends EditorDialog { public ObjectEditorDialog (EditorContext ctx, EditorScenePanel panel) { - super("Edit object attributes", true); - _ctx = ctx; - _panel = panel; - - // get a handle on the top-level panel - JPanel top = (JPanel)getContentPane(); - - // set up a layout manager for the panel - VGroupLayout gl = new VGroupLayout( - VGroupLayout.STRETCH, VGroupLayout.STRETCH, 5, VGroupLayout.CENTER); - top.setLayout(gl); - top.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); + super("Edit object attributes", ctx, panel); + } + @Override + public void addComponents (JComponent panel) + { // object action editor elements JPanel sub = new JPanel(new HGroupLayout(HGroupLayout.STRETCH)); sub.add(new JLabel("Object action command:"), HGroupLayout.FIXED); sub.add(_action = new JTextField()); _action.addActionListener(this); _action.setActionCommand("ok"); - top.add(sub); + panel.add(sub); // create the priority slider sub = new JPanel(new HGroupLayout(HGroupLayout.STRETCH)); @@ -86,28 +73,21 @@ public class ObjectEditorDialog extends JInternalFrame _priority.setMajorTickSpacing(5); _priority.setMinorTickSpacing(1); _priority.setPaintTicks(true); - top.add(sub); + panel.add(sub); // create colorization selectors JPanel zations = HGroupLayout.makeButtonBox(HGroupLayout.LEFT); zations.add(new JLabel("Colorizations:")); zations.add(_primary = new JComboBox(NO_CHOICES)); zations.add(_secondary = new JComboBox(NO_CHOICES)); - top.add(zations); + panel.add(zations); - // create our OK/Cancel buttons - sub = HGroupLayout.makeButtonBox(HGroupLayout.CENTER); - EditorDialogUtil.addButton(this, sub, "OK", "ok"); - EditorDialogUtil.addButton(this, sub, "Cancel", "cancel"); - top.add(sub); - - pack(); } /** - * Prepare the dialog for display. This method should be called - * before display() is called. - * + * Prepare the dialog for display. This method should be called before display() + * is called. + * * @param scobj the object to edit. */ public void prepare (SceneObject scobj) @@ -125,8 +105,7 @@ public class ObjectEditorDialog extends JInternalFrame } catch (NoSuchTileSetException nstse) { title = "Error(" + tsid + "): " + tidx; } - title += " (" + StringUtil.coordsToString( - _scobj.info.x, _scobj.info.y) + ")"; + title += " (" + StringUtil.coordsToString(_scobj.info.x, _scobj.info.y) + ")"; setTitle(title); // configure our elements @@ -135,7 +114,6 @@ public class ObjectEditorDialog extends JInternalFrame _priority.setValue(scobj.getPriority()); // if the object supports colorizations, configure those - boolean haveZations = false; Object[] pzations = null; Object[] szations = null; if (tset != null) { @@ -149,8 +127,7 @@ public class ObjectEditorDialog extends JInternalFrame } } configureZations(_primary, pzations, _scobj.info.getPrimaryZation()); - configureZations(_secondary, szations, - _scobj.info.getSecondaryZation()); + configureZations(_secondary, szations, _scobj.info.getSecondaryZation()); // select the text edit field and focus it _action.setCaretPosition(0); @@ -168,18 +145,16 @@ public class ObjectEditorDialog extends JInternalFrame if (crecs == null) { return null; } - Object[] czations = new Object[crecs.length+1]; + Object[] czations = new Object[crecs.length + 1]; czations[0] = new ZationChoice(0, "none"); for (int ii = 0; ii < crecs.length; ii++) { - czations[ii+1] = - new ZationChoice(crecs[ii].colorId, crecs[ii].name); + czations[ii + 1] = new ZationChoice(crecs[ii].colorId, crecs[ii].name); } Arrays.sort(czations); return czations; } - protected void configureZations ( - JComboBox combo, Object[] zations, int colorId) + protected void configureZations (JComboBox combo, Object[] zations, int colorId) { int selidx = 0; combo.setEnabled(zations != null); @@ -195,39 +170,32 @@ public class ObjectEditorDialog extends JInternalFrame combo.setSelectedIndex(selidx); } - // documentation inherited from interface - public void actionPerformed (ActionEvent e) + @Override + public void accepted () { - String cmd = e.getActionCommand(); - if (cmd.equals("ok")) { - _scobj.info.action = _action.getText(); - byte prio = (byte)_priority.getValue(); - if (prio != _scobj.getPriority()) { - _scobj.setPriority(prio); - } - - int ozations = _scobj.info.zations; - ZationChoice pchoice = (ZationChoice)_primary.getSelectedItem(); - ZationChoice schoice = (ZationChoice)_secondary.getSelectedItem(); - _scobj.info.setZations(pchoice.colorId, schoice.colorId); - if (ozations != _scobj.info.zations) { - _scobj.refreshObjectTile(_panel); - } - - _panel.objectEditorDismissed(); - - } else if (cmd.equals("cancel")) { - // do nothing except hide the dialog - _panel.objectEditorDismissed(); - - } else { - System.err.println("Received unknown action: " + e); - return; + _scobj.info.action = _action.getText(); + byte prio = (byte)_priority.getValue(); + if (prio != _scobj.getPriority()) { + _scobj.setPriority(prio); } - // hide the dialog - EditorDialogUtil.dismiss(this); + int ozations = _scobj.info.zations; + ZationChoice pchoice = (ZationChoice)_primary.getSelectedItem(); + ZationChoice schoice = (ZationChoice)_secondary.getSelectedItem(); + _scobj.info.setZations(pchoice.colorId, schoice.colorId); + if (ozations != _scobj.info.zations) { + _scobj.refreshObjectTile(_panel); + } + + _panel.objectEditorDismissed(); + } + + @Override + public void cancelled () + { + _panel.objectEditorDismissed(); + } /** Used to display colorization choices. */ @@ -237,27 +205,28 @@ public class ObjectEditorDialog extends JInternalFrame public short colorId; public String name; - public ZationChoice (int colorId, String name) { + public ZationChoice (int colorId, String name) + { this.colorId = (short)colorId; this.name = name; } - public int compareTo (Object other) { + public int compareTo (Object other) + { return colorId - ((ZationChoice)other).colorId; } - public String toString () { + public String toString () + { return name; } } - protected EditorContext _ctx; - protected EditorScenePanel _panel; protected JTextField _action; protected JSlider _priority; protected SceneObject _scobj; protected JComboBox _primary, _secondary; - protected static final ZationChoice[] NO_CHOICES = new ZationChoice[] { - new ZationChoice(0, "none") }; + protected static final ZationChoice[] NO_CHOICES = new ZationChoice[] { new ZationChoice(0, + "none") }; } diff --git a/src/java/com/threerings/stage/tools/editor/PortalDialog.java b/src/java/com/threerings/stage/tools/editor/PortalDialog.java index 499500a5..19cb8164 100644 --- a/src/java/com/threerings/stage/tools/editor/PortalDialog.java +++ b/src/java/com/threerings/stage/tools/editor/PortalDialog.java @@ -21,44 +21,41 @@ package com.threerings.stage.tools.editor; -import java.awt.*; -import java.awt.event.*; -import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; + +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.SwingConstants; import com.samskivert.swing.HGroupLayout; -import com.samskivert.swing.VGroupLayout; - +import com.threerings.stage.data.StageScene; +import com.threerings.stage.tools.editor.util.EditorContext; import com.threerings.whirled.spot.data.Portal; import com.threerings.whirled.spot.tools.EditablePortal; -import com.threerings.stage.data.StageScene; -import com.threerings.stage.tools.editor.util.EditorDialogUtil; - /** * The PortalDialog is used to present the user with a dialog * allowing them to enter the information associated with an * EditablePortal. The dialog is used both to set up a new * portal and to edit an existing portal. */ -public class PortalDialog extends JInternalFrame - implements ActionListener +public class PortalDialog extends EditorDialog { /** * Constructs the portal dialog. */ - public PortalDialog () + public PortalDialog (EditorContext ctx, EditorScenePanel panel) { - super("Edit Portal", true); - - // get a handle on the top-level panel - JPanel top = (JPanel)getContentPane(); - - // set up a layout manager for the panel - VGroupLayout gl = new VGroupLayout(VGroupLayout.STRETCH); - gl.setOffAxisPolicy(VGroupLayout.STRETCH); - top.setLayout(gl); - top.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); - + super("Edit Portal", ctx, panel); + } + + @Override + public void addComponents(JComponent top){ // add the dialog instruction text top.add(new JLabel("Enter settings for this portal:")); @@ -78,15 +75,6 @@ public class PortalDialog extends JInternalFrame _entrance.addActionListener(this); _entrance.setActionCommand("entrance"); top.add(_entrance); - - // create a panel to contain the OK/Cancel buttons - sub = new JPanel(new HGroupLayout()); - EditorDialogUtil.addButton(this, sub, "OK", "ok"); - - // add the buttons to the top-level panel - top.add(sub); - - pack(); } /** @@ -122,25 +110,21 @@ public class PortalDialog extends JInternalFrame /** * Handle action events on the dialog user interface elements. */ + @Override public void actionPerformed (ActionEvent e) { - String cmd = e.getActionCommand(); - - if (cmd.equals("entrance")) { + if (e.getActionCommand().equals("entrance")) { _entrance.setSelected(_entrance.isSelected()); - - } else if (cmd.equals("ok")) { - handleSubmit(); - } else { - Log.warning("Unknown action command [cmd=" + cmd + "]."); + super.actionPerformed(e); } } /** * Handles the user submitting the dialog via the "OK" button. */ - protected void handleSubmit () + @Override + public void accepted () { // get the destination scene name _port.name = _portalText.getText(); @@ -152,16 +136,13 @@ public class PortalDialog extends JInternalFrame } else if (_scene.getDefaultEntrance() == _port) { _scene.setDefaultEntrance(null); } - - // hide the dialog - EditorDialogUtil.dismiss(this); } // documentation inherited protected void processKeyEvent (KeyEvent e) { switch (e.getKeyCode()) { - case KeyEvent.VK_ENTER: handleSubmit(); break; + case KeyEvent.VK_ENTER: accepted(); break; case KeyEvent.VK_ESCAPE: setVisible(false); break; } }