//Copyright (c) 2007 University of Auckland
//Contributed to the FBench extension of the OOONEIDA Workbench project under the Common Public License
package fbench.graph.popup;
import static javax.swing.GroupLayout.Alignment.CENTER;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import fbench.LibraryElementView;
import fbench.graph.GraphView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
/**
* An abstract class for dialogs of various purposes.
* @author JP
* @version 20070222/JP
* gsha041: Changed to public for plugins
*/
public abstract class DialogModel extends JDialog{
protected GroupLayout layout;
protected DialogModel me = this;
protected Document document;
private JFrame ownerFrame;
private MouseEvent mouseEvt;
private Element element;
protected Float scaleFactor = Float.parseFloat(
""+((getFontMetrics(getFont()).getHeight() + 4.0) / 100.0));
/**
* Create components belong to <B>this</B> dialog
* @param command String value of the selected context menu item of GVContextMenu.
*/
public abstract void create(String command);
public DialogModel(Element element, MouseEvent mouseEvt){
super(getOwnerFrame(mouseEvt));
JFrame owner = getOwnerFrame(mouseEvt);
setOwnerFrame(owner);
setElement(element);
setMouseEvent(mouseEvt);
setResizable(false);
setModal(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
document = element.getOwnerDocument();
}
protected Element rearrangeElement(){
Element graphElement = getElement();
Element modelElement = getModelElement();
setElement(modelElement);
return graphElement;
}
private static JFrame getOwnerFrame(MouseEvent evt){
Container parent = evt.getComponent().getParent();
while(!(parent instanceof JFrame))
parent = parent.getParent();
return (JFrame)parent;
}
protected void setDialogSize(int width, int height){
setSize(width, height);
setLocation(ownerFrame.getX()+ownerFrame.getWidth()/2-width/2,
ownerFrame.getY()+ownerFrame.getHeight()/2-height/2);
}
protected void setDialogSize(Dimension size){
setSize(size);
setLocation(ownerFrame.getX()+ownerFrame.getWidth()/2-size.width/2,
ownerFrame.getY()+ownerFrame.getHeight()/2-size.height/2);
}
protected void setComponentSize( JComponent comp, Dimension minimumSize, Dimension maximumSize){
comp.setMinimumSize(minimumSize);
comp.setMaximumSize(maximumSize);
}
protected void setComponentSize( JComponent comp, Dimension newSize){
comp.setMinimumSize(newSize);
comp.setMaximumSize(newSize);
}
protected GraphView getGraphView(){
if(mouseEvt.getComponent().getClass().toString().equals("class fbench.graph.GraphView"))
return (GraphView) mouseEvt.getComponent();
Container parent = mouseEvt.getComponent().getParent();
while(parent != null && !parent.getClass().toString()
.equals("class fbench.graph.GraphView")){
parent = parent.getParent();
}
return (GraphView)parent;
}
protected LibraryElementView getLibraryElementView(){
System.out.println(mouseEvt.getSource());
Container parent = mouseEvt.getComponent().getParent();
while(parent != null && !parent.getClass().equals(LibraryElementView.class)){
System.out.println(parent.getClass().toString());
parent = parent.getParent();
}
return (LibraryElementView)parent;
}
protected Element getModelElement(){
Component cp = getMouseEvent().getComponent();
while( !(cp.getParent() instanceof GraphView) )
{
if( cp.getParent() == null)
return null;
cp = cp.getParent();
}
return ((GraphView)cp.getParent()).getModel().getElement();
}
protected void printDocument(){
NodeList nl = document.getDocumentElement().getChildNodes();
iterateNodeList(nl);
}
private String tab = "";
private void iterateNodeList(NodeList nl){
tab = tab.concat(" ");
for(int i = 0; i < nl.getLength(); i++){
Element el = (Element)nl.item(i);
System.out.println(tab+el.getNodeName()+" : "+el.getAttribute("Name"));
iterateNodeList(nl.item(i).getChildNodes());
}
tab = tab.replaceFirst(" ", "");
}
public void createRemoveDialog(){
setTitle("Delete");
JLabel queryLabel = new JLabel("Delete "+getElement().getNodeName()
+" : "+getElement().getAttribute("Name")+"?");
JButton acceptButton = new JButton("Yes");
JButton cancelButton = new JButton("No");
acceptButton.addActionListener(new AcceptDeleteButtonListener());
cancelButton.addActionListener(new CancelButtonActionListener());
layout.linkSize(SwingConstants.HORIZONTAL, acceptButton, cancelButton);
layout.setHorizontalGroup(layout.createParallelGroup(CENTER)
.addComponent(queryLabel)
.addGroup(layout.createSequentialGroup()
.addComponent(acceptButton)
.addComponent(cancelButton)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(queryLabel)
.addGap(10)
.addGroup(layout.createParallelGroup()
.addComponent(acceptButton)
.addComponent(cancelButton)));
getContentPane().validate();
getContentPane().doLayout();
setResizable(true);
int width = Math.max(queryLabel.getX() + queryLabel.getWidth(),
cancelButton.getX() + cancelButton.getWidth()) +
(Math.min(queryLabel.getX(), acceptButton.getX()) * 2) -
(cancelButton.getX() - (acceptButton.getX() + acceptButton.getWidth()));
int height = acceptButton.getY() + acceptButton.getHeight() + queryLabel.getX() * 3;
Dimension dialogSize = new Dimension(width, height);
setDialogSize(dialogSize);
}
protected Element createNewElement(String nodeName){
return element.getOwnerDocument().createElement(nodeName);
}
protected void setElement(Element newElement){
element = newElement;
}
protected Element getElement(){
return element;
}
private void setOwnerFrame(JFrame newOwnerFrame){
ownerFrame = newOwnerFrame;
}
private void setMouseEvent(MouseEvent newMouseEvent){
mouseEvt = newMouseEvent;
}
public MouseEvent getMouseEvent(){
return mouseEvt;
}
private class AcceptDeleteButtonListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
deleteElement(document.getDocumentElement().getChildNodes());
GraphView gv = getGraphView();
gv.setElement(getModelElement());
LibraryElementView lev = (LibraryElementView)(gv.getParent().getParent().getParent().getParent().getParent());
lev.getTextModel().setElement(document.getDocumentElement());
//getGraphView().setElement(getModelElement());
dispose();
}
/**
* Recursive method for deleting the selected element from the document
* @param Nodelist of the top-level element
* @return boolean which represents whether the element has been deleted or not
*/
private boolean deleteElement(NodeList nl){
for(int i = 0; i < nl.getLength(); i++){
Element el = (Element)nl.item(i);
if(deleteElement(nl.item(i).getChildNodes()))
return true;
if (el.equals(getElement())){
el.getParentNode().removeChild(el);
return true;
}
}
return false;
}
}
// gsha041: Changed to public for plugins
public class CancelButtonActionListener implements ActionListener{
public void actionPerformed(ActionEvent evt) {
me.dispose();
}
}
}