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.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;
@@ -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
@@ -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,27 +73,20 @@ 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 <code>display()</code> is called.
* Prepare the dialog for display. This method should be called before <code>display()</code>
* is called.
*
* @param scobj the object to edit.
*/
@@ -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,12 +170,10 @@ 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()) {
@@ -216,18 +189,13 @@ public class ObjectEditorDialog extends JInternalFrame
}
_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
EditorDialogUtil.dismiss(this);
@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") };
}
@@ -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 <code>PortalDialog</code> is used to present the user with a dialog
* allowing them to enter the information associated with an
* <code>EditablePortal</code>. 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;
}
}