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
This commit is contained in:
Charlie Groves
2008-03-26 03:23:40 +00:00
parent 29460ce450
commit 99205455e7
5 changed files with 152 additions and 132 deletions
@@ -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;
}
@@ -23,7 +23,6 @@ package com.threerings.stage.tools.editor;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@@ -31,7 +30,6 @@ import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener; import java.awt.event.MouseWheelListener;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.io.File; import java.io.File;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
@@ -48,19 +46,16 @@ import com.samskivert.swing.GroupLayout;
import com.samskivert.swing.HGroupLayout; import com.samskivert.swing.HGroupLayout;
import com.samskivert.swing.util.MenuUtil; import com.samskivert.swing.util.MenuUtil;
import com.samskivert.swing.util.SwingUtil; import com.samskivert.swing.util.SwingUtil;
import com.threerings.media.ManagedJFrame; import com.threerings.media.ManagedJFrame;
import com.threerings.miso.tile.BaseTileSet; import com.threerings.miso.tile.BaseTileSet;
import com.threerings.miso.util.MisoSceneMetrics; import com.threerings.miso.util.MisoSceneMetrics;
import com.threerings.stage.data.StageScene; import com.threerings.stage.data.StageScene;
import com.threerings.stage.data.StageSceneModel; import com.threerings.stage.data.StageSceneModel;
import com.threerings.stage.tools.editor.util.EditorContext; import com.threerings.stage.tools.editor.util.EditorContext;
import com.threerings.stage.tools.editor.util.EditorDialogUtil; import com.threerings.stage.tools.editor.util.EditorDialogUtil;
import com.threerings.stage.tools.xml.StageSceneParser; import com.threerings.stage.tools.xml.StageSceneParser;
import com.threerings.stage.tools.xml.StageSceneWriter; import com.threerings.stage.tools.xml.StageSceneWriter;
import com.threerings.whirled.tools.xml.SceneParser;
public class EditorFrame extends ManagedJFrame public class EditorFrame extends ManagedJFrame
{ {
@@ -580,7 +575,7 @@ public class EditorFrame extends ManagedJFrame
protected EditorContext _ctx; protected EditorContext _ctx;
/** We use this to load scenes. */ /** We use this to load scenes. */
protected StageSceneParser _parser = new StageSceneParser(); protected SceneParser _parser = new StageSceneParser();
/** We use this to save scenes. */ /** We use this to save scenes. */
protected StageSceneWriter _writer; protected StageSceneWriter _writer;
@@ -335,7 +335,7 @@ public class EditorScenePanel extends StageScenePanel
{ {
// create our portal dialog if we haven't yet // create our portal dialog if we haven't yet
if (_dialogPortal == null) { if (_dialogPortal == null) {
_dialogPortal = new PortalDialog(); _dialogPortal = new PortalDialog(_ctx, this);
} }
// pass location information on to the dialog // pass location information on to the dialog
@@ -12,7 +12,7 @@
// //
// This library is distributed in the hope that it will be useful, // This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // 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. // Lesser General Public License for more details.
// //
// You should have received a copy of the GNU Lesser General Public // You should have received a copy of the GNU Lesser General Public
@@ -21,20 +21,16 @@
package com.threerings.stage.tools.editor; package com.threerings.stage.tools.editor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays; import java.util.Arrays;
import javax.swing.BorderFactory;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.JInternalFrame; import javax.swing.JComponent;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JSlider; import javax.swing.JSlider;
import javax.swing.JTextField; import javax.swing.JTextField;
import com.samskivert.swing.HGroupLayout; import com.samskivert.swing.HGroupLayout;
import com.samskivert.swing.VGroupLayout;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
import com.threerings.media.image.ColorPository.ColorRecord; 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.miso.client.SceneObject;
import com.threerings.stage.tools.editor.util.EditorContext; import com.threerings.stage.tools.editor.util.EditorContext;
import com.threerings.stage.tools.editor.util.EditorDialogUtil;
/** /**
* Used to edit object attributes. * Used to edit object attributes.
*/ */
public class ObjectEditorDialog extends JInternalFrame public class ObjectEditorDialog extends EditorDialog
implements ActionListener
{ {
public ObjectEditorDialog (EditorContext ctx, EditorScenePanel panel) public ObjectEditorDialog (EditorContext ctx, EditorScenePanel panel)
{ {
super("Edit object attributes", true); super("Edit object attributes", ctx, panel);
_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));
@Override
public void addComponents (JComponent panel)
{
// object action editor elements // object action editor elements
JPanel sub = new JPanel(new HGroupLayout(HGroupLayout.STRETCH)); JPanel sub = new JPanel(new HGroupLayout(HGroupLayout.STRETCH));
sub.add(new JLabel("Object action command:"), HGroupLayout.FIXED); sub.add(new JLabel("Object action command:"), HGroupLayout.FIXED);
sub.add(_action = new JTextField()); sub.add(_action = new JTextField());
_action.addActionListener(this); _action.addActionListener(this);
_action.setActionCommand("ok"); _action.setActionCommand("ok");
top.add(sub); panel.add(sub);
// create the priority slider // create the priority slider
sub = new JPanel(new HGroupLayout(HGroupLayout.STRETCH)); sub = new JPanel(new HGroupLayout(HGroupLayout.STRETCH));
@@ -86,28 +73,21 @@ public class ObjectEditorDialog extends JInternalFrame
_priority.setMajorTickSpacing(5); _priority.setMajorTickSpacing(5);
_priority.setMinorTickSpacing(1); _priority.setMinorTickSpacing(1);
_priority.setPaintTicks(true); _priority.setPaintTicks(true);
top.add(sub); panel.add(sub);
// create colorization selectors // create colorization selectors
JPanel zations = HGroupLayout.makeButtonBox(HGroupLayout.LEFT); JPanel zations = HGroupLayout.makeButtonBox(HGroupLayout.LEFT);
zations.add(new JLabel("Colorizations:")); zations.add(new JLabel("Colorizations:"));
zations.add(_primary = new JComboBox(NO_CHOICES)); zations.add(_primary = new JComboBox(NO_CHOICES));
zations.add(_secondary = 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 * Prepare the dialog for display. This method should be called before <code>display()</code>
* before <code>display()</code> is called. * is called.
* *
* @param scobj the object to edit. * @param scobj the object to edit.
*/ */
public void prepare (SceneObject scobj) public void prepare (SceneObject scobj)
@@ -125,8 +105,7 @@ public class ObjectEditorDialog extends JInternalFrame
} catch (NoSuchTileSetException nstse) { } catch (NoSuchTileSetException nstse) {
title = "Error(" + tsid + "): " + tidx; title = "Error(" + tsid + "): " + tidx;
} }
title += " (" + StringUtil.coordsToString( title += " (" + StringUtil.coordsToString(_scobj.info.x, _scobj.info.y) + ")";
_scobj.info.x, _scobj.info.y) + ")";
setTitle(title); setTitle(title);
// configure our elements // configure our elements
@@ -135,7 +114,6 @@ public class ObjectEditorDialog extends JInternalFrame
_priority.setValue(scobj.getPriority()); _priority.setValue(scobj.getPriority());
// if the object supports colorizations, configure those // if the object supports colorizations, configure those
boolean haveZations = false;
Object[] pzations = null; Object[] pzations = null;
Object[] szations = null; Object[] szations = null;
if (tset != null) { if (tset != null) {
@@ -149,8 +127,7 @@ public class ObjectEditorDialog extends JInternalFrame
} }
} }
configureZations(_primary, pzations, _scobj.info.getPrimaryZation()); configureZations(_primary, pzations, _scobj.info.getPrimaryZation());
configureZations(_secondary, szations, configureZations(_secondary, szations, _scobj.info.getSecondaryZation());
_scobj.info.getSecondaryZation());
// select the text edit field and focus it // select the text edit field and focus it
_action.setCaretPosition(0); _action.setCaretPosition(0);
@@ -168,18 +145,16 @@ public class ObjectEditorDialog extends JInternalFrame
if (crecs == null) { if (crecs == null) {
return null; return null;
} }
Object[] czations = new Object[crecs.length+1]; Object[] czations = new Object[crecs.length + 1];
czations[0] = new ZationChoice(0, "none"); czations[0] = new ZationChoice(0, "none");
for (int ii = 0; ii < crecs.length; ii++) { for (int ii = 0; ii < crecs.length; ii++) {
czations[ii+1] = czations[ii + 1] = new ZationChoice(crecs[ii].colorId, crecs[ii].name);
new ZationChoice(crecs[ii].colorId, crecs[ii].name);
} }
Arrays.sort(czations); Arrays.sort(czations);
return czations; return czations;
} }
protected void configureZations ( protected void configureZations (JComboBox combo, Object[] zations, int colorId)
JComboBox combo, Object[] zations, int colorId)
{ {
int selidx = 0; int selidx = 0;
combo.setEnabled(zations != null); combo.setEnabled(zations != null);
@@ -195,39 +170,32 @@ public class ObjectEditorDialog extends JInternalFrame
combo.setSelectedIndex(selidx); combo.setSelectedIndex(selidx);
} }
// documentation inherited from interface @Override
public void actionPerformed (ActionEvent e) public void accepted ()
{ {
String cmd = e.getActionCommand();
if (cmd.equals("ok")) { _scobj.info.action = _action.getText();
_scobj.info.action = _action.getText(); byte prio = (byte)_priority.getValue();
byte prio = (byte)_priority.getValue(); if (prio != _scobj.getPriority()) {
if (prio != _scobj.getPriority()) { _scobj.setPriority(prio);
_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;
} }
// hide the dialog int ozations = _scobj.info.zations;
EditorDialogUtil.dismiss(this); 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. */ /** Used to display colorization choices. */
@@ -237,27 +205,28 @@ public class ObjectEditorDialog extends JInternalFrame
public short colorId; public short colorId;
public String name; public String name;
public ZationChoice (int colorId, String name) { public ZationChoice (int colorId, String name)
{
this.colorId = (short)colorId; this.colorId = (short)colorId;
this.name = name; this.name = name;
} }
public int compareTo (Object other) { public int compareTo (Object other)
{
return colorId - ((ZationChoice)other).colorId; return colorId - ((ZationChoice)other).colorId;
} }
public String toString () { public String toString ()
{
return name; return name;
} }
} }
protected EditorContext _ctx;
protected EditorScenePanel _panel;
protected JTextField _action; protected JTextField _action;
protected JSlider _priority; protected JSlider _priority;
protected SceneObject _scobj; protected SceneObject _scobj;
protected JComboBox _primary, _secondary; protected JComboBox _primary, _secondary;
protected static final ZationChoice[] NO_CHOICES = new ZationChoice[] { protected static final ZationChoice[] NO_CHOICES = new ZationChoice[] { new ZationChoice(0,
new ZationChoice(0, "none") }; "none") };
} }
@@ -21,44 +21,41 @@
package com.threerings.stage.tools.editor; package com.threerings.stage.tools.editor;
import java.awt.*; import java.awt.event.ActionEvent;
import java.awt.event.*; import java.awt.event.KeyEvent;
import javax.swing.*;
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.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.data.Portal;
import com.threerings.whirled.spot.tools.EditablePortal; import com.threerings.whirled.spot.tools.EditablePortal;
import com.threerings.stage.data.StageScene;
import com.threerings.stage.tools.editor.util.EditorDialogUtil;
/** /**
* The <code>PortalDialog</code> is used to present the user with a dialog * The <code>PortalDialog</code> is used to present the user with a dialog
* allowing them to enter the information associated with an * allowing them to enter the information associated with an
* <code>EditablePortal</code>. The dialog is used both to set up a new * <code>EditablePortal</code>. The dialog is used both to set up a new
* portal and to edit an existing portal. * portal and to edit an existing portal.
*/ */
public class PortalDialog extends JInternalFrame public class PortalDialog extends EditorDialog
implements ActionListener
{ {
/** /**
* Constructs the portal dialog. * Constructs the portal dialog.
*/ */
public PortalDialog () public PortalDialog (EditorContext ctx, EditorScenePanel panel)
{ {
super("Edit Portal", true); super("Edit Portal", ctx, panel);
}
// get a handle on the top-level panel
JPanel top = (JPanel)getContentPane(); @Override
public void addComponents(JComponent top){
// 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));
// add the dialog instruction text // add the dialog instruction text
top.add(new JLabel("Enter settings for this portal:")); top.add(new JLabel("Enter settings for this portal:"));
@@ -78,15 +75,6 @@ public class PortalDialog extends JInternalFrame
_entrance.addActionListener(this); _entrance.addActionListener(this);
_entrance.setActionCommand("entrance"); _entrance.setActionCommand("entrance");
top.add(_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. * Handle action events on the dialog user interface elements.
*/ */
@Override
public void actionPerformed (ActionEvent e) public void actionPerformed (ActionEvent e)
{ {
String cmd = e.getActionCommand(); if (e.getActionCommand().equals("entrance")) {
if (cmd.equals("entrance")) {
_entrance.setSelected(_entrance.isSelected()); _entrance.setSelected(_entrance.isSelected());
} else if (cmd.equals("ok")) {
handleSubmit();
} else { } else {
Log.warning("Unknown action command [cmd=" + cmd + "]."); super.actionPerformed(e);
} }
} }
/** /**
* Handles the user submitting the dialog via the "OK" button. * Handles the user submitting the dialog via the "OK" button.
*/ */
protected void handleSubmit () @Override
public void accepted ()
{ {
// get the destination scene name // get the destination scene name
_port.name = _portalText.getText(); _port.name = _portalText.getText();
@@ -152,16 +136,13 @@ public class PortalDialog extends JInternalFrame
} else if (_scene.getDefaultEntrance() == _port) { } else if (_scene.getDefaultEntrance() == _port) {
_scene.setDefaultEntrance(null); _scene.setDefaultEntrance(null);
} }
// hide the dialog
EditorDialogUtil.dismiss(this);
} }
// documentation inherited // documentation inherited
protected void processKeyEvent (KeyEvent e) protected void processKeyEvent (KeyEvent e)
{ {
switch (e.getKeyCode()) { switch (e.getKeyCode()) {
case KeyEvent.VK_ENTER: handleSubmit(); break; case KeyEvent.VK_ENTER: accepted(); break;
case KeyEvent.VK_ESCAPE: setVisible(false); break; case KeyEvent.VK_ESCAPE: setVisible(false); break;
} }
} }