// File: ConstVertPartReorderDialog.java // Classes: ConstVertPartReorderDialog // Author: Kedar Patankar package edu.neu.ccs.demeter.tools.apstudio.graphedit; import java.awt.Frame; import java.awt.Panel; import java.awt.Button; import java.awt.List; import java.awt.Event; import java.awt.BorderLayout; import java.awt.Point; import java.awt.Dimension; import java.awt.Dialog; import java.awt.Toolkit; import java.awt.Insets; import java.util.Vector; import javax.swing.JButton; import javax.swing.ImageIcon; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; /* Purpose : Allow to reorder parts of construction class */ public class ConstVertPartReorderDialog extends Dialog implements ActionListener { // Components private Button _ok,_cancel; private List _partList; private JButton _up,_down; private int _noOfItems; private Vector v; private boolean _changed=false; //references private Editor _editor; private UVertex _vertex; // Constructor public ConstVertPartReorderDialog(Frame frame,String title,UVertex _v) { super(frame,title,true); _editor=(Editor)frame; String INSTALL_DIR=_editor.getInstallDir(); setLayout( new BorderLayout() ); _vertex=_v; // Create the Buttons Panel button_panel = new Panel(); _ok=new Button("Ok"); _cancel=new Button("Cancel"); _ok.addActionListener(this); _cancel.addActionListener(this); button_panel.add(_ok); button_panel.add(_cancel); Panel arrow_panel = new Panel(); arrow_panel.setLayout( new BorderLayout() ); _up = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(INSTALL_DIR+"/images/up.gif"))); _up.setToolTipText("Up"); _up.setMargin(new Insets(0,0,0,0)); _up.addActionListener(this); _down = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(INSTALL_DIR+"/images/down.gif"))); _down.setToolTipText("Down"); _down.setMargin(new Insets(0,0,0,0)); _down.addActionListener(this); arrow_panel.add("North",_down); arrow_panel.add("South",_up); // Create the text area _partList = new List(5,false); // add the text area and the buttons to the window add("West",_partList); add("East",arrow_panel); add("South",button_panel ); pack(); // jdk 1.1 type window event handling addWindowListener(new TVAdapter()); // using inner class for window handling. v = new Vector(); Document d =_editor.curDocument(); if(_v instanceof UConstVertex) { for(int i=0;i<_v.get_outArcIdList().size();i++) { v.addElement((UID)_v.get_outArcIdList().elementAt(i)); UEdge e = d.net().getArc(((UID)_v.get_outArcIdList().elementAt(i))); String to = d.net().getNode(e.get_toVertex()).get_vertexname().get_name().toString(); _partList.addItem(to); } } _noOfItems=_partList.getItemCount(); if(_noOfItems != 0) _partList.select(0); if(_noOfItems<2) { _up.setEnabled(false); _down.setEnabled(false); } } private void close() { setVisible(false);dispose(); } public void setVisible(boolean status) { if(status) { _ok.requestFocus(); Dimension frameSize = getParent().getSize(); Point frameLoc = getParent().getLocation(); Dimension mySize = getSize(); int x,y; x = frameLoc.x + (frameSize.width/2) -(mySize.width/2); y = frameLoc.y + (frameSize.height/2)-(mySize.height/2); setBounds(x,y,getSize().width,getSize().height); } super.setVisible(status); } // jdk 1.1 style event handling. public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if(source == _up) { int current = _partList.getSelectedIndex(); if(current == 0) return; else { int next = current-1; String toBeMovedUp = _partList.getItem(current); String toBeMovedDown = _partList.getItem(next); _partList.replaceItem(toBeMovedUp,next); _partList.replaceItem(toBeMovedDown,current); _partList.deselect(current); _partList.select(next); Object o = v.elementAt(current); v.setElementAt(v.elementAt(next),current); v.setElementAt(o,next); _changed=true; } } else if(source == _down) { int current = _partList.getSelectedIndex(); if(current == _noOfItems-1) return; else { int next = current+1; String toBeMovedDown = _partList.getItem(current); String toBeMovedUp = _partList.getItem(next); _partList.deselect(current); _partList.replaceItem(toBeMovedUp,current); _partList.replaceItem(toBeMovedDown,next); _partList.select(next); Object o = v.elementAt(current); v.setElementAt(v.elementAt(next),current); v.setElementAt(o,next); _changed=true; } } else if(source == _ok) { if(_changed) { _vertex.set_outArcIdList(v); _editor.curDocument().cdNeedsSaving(true); } close(); } else close(); } // inner class VDAdapter class TVAdapter extends WindowAdapter { public void windowClosing(WindowEvent event) { close(); } } } /* end class TextView */