Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 787 → Rev 788

/sun/xmleditor/trunk/src/plugins/regionEditorPlugin/Region.java
4,10 → 4,10
* Coded by: Group 5, software practice summer 2003
* University of Bielefeld, Germany
*
* @version $Revision: 1.3 $
* @version $Revision: 1.4 $
*
* Last modification: $Date: 2003/07/23 19:42:52 $
* $Id: Region.java,v 1.3 2003/07/23 19:42:52 ioklasse Exp $
* Last modification: $Date: 2003/07/23 23:39:27 $
* $Id: Region.java,v 1.4 2003/07/23 23:39:27 ioklasse Exp $
*/
 
package src.plugins.regionEditorPlugin;
28,6 → 28,9
/** Stores coordinates of region. */
private Rectangle rect;
 
/** Stores name of node for the region.*/
private String savedNameOfNode;
 
/** Creates empty region with coordinates and name. */
public Region()
{
45,7 → 48,7
}
 
/** Returns the name of the region.
*
*
* @return the name of the region.
*/
public String getName()
54,7 → 57,7
}
 
/** Sets the name of the region.
*
*
* @param name: The name of the region.
*/
public void setName(String name)
63,7 → 66,7
}
 
/** Returns the coordinates of the region.
*
*
* @return the coordinates of the region.
*/
public Rectangle getRect()
72,7 → 75,7
}
 
/** Sets the coordinates of the region.
*
*
* @param rect: The coordinates of the region.
*/
public void setRect(Rectangle rect)
79,4 → 82,22
{
this.rect = rect;
}
 
/** Returns the saved name of node for the region.
*
* @return the saved name of node for the region.
*/
public String getSavedNameOfNode()
{
return savedNameOfNode;
}
 
/** Sets the saved name of node for the region.
*
* @param name: The saved name of node for the region.
*/
public void setSavedNameOfNode(String name)
{
this.savedNameOfNode = name;
}
}
/sun/xmleditor/trunk/src/plugins/regionEditorPlugin/RegionEditorPlugin.java
4,10 → 4,10
* Coded by: Group 5, software practice summer 2003
* University of Bielefeld, Germany
*
* @version $Revision: 1.26 $
* @version $Revision: 1.27 $
*
* Last modification: $Date: 2003/07/23 20:09:50 $
* $Id: RegionEditorPlugin.java,v 1.26 2003/07/23 20:09:50 ioklasse Exp $
* Last modification: $Date: 2003/07/23 23:39:27 $
* $Id: RegionEditorPlugin.java,v 1.27 2003/07/23 23:39:27 ioklasse Exp $
*/
 
package src.plugins.regionEditorPlugin;
14,6 → 14,7
 
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
 
import java.awt.BorderLayout;
import java.awt.Cursor;
37,11 → 38,11
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
 
import javax.swing.JButton;
//import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
52,15 → 53,16
import src.control.OurEventListener;
import src.gui.*;
import src.plugins.PluginInterface;
import src.parser.AdapterNode;
import src.plugins.PluginManagerInterface;
 
 
 
/**
* The <code>RegionEditor</code> plugin shows and edits image regions.
*
* @author Yulia Klassen, Viktoriya Zudova
*
* @version $Revision: 1.26 $ Last modification: $Date: 2003/07/23 20:09:50 $
* @version $Revision: 1.27 $ Last modification: $Date: 2003/07/23 23:39:27 $
*/
public class RegionEditorPlugin
extends JPanel
71,7 → 73,7
 
/** The current image. */
private Image image;
 
/** Label where the name of image is. */
private JLabel imageNameLabel = new JLabel(" Image Name ");
 
114,6 → 116,9
/** The node returned to the basis program. */
private Node changedNode;
 
/** The document */
private Document environment;
 
/** The list of all regions. */
private List regions = new ArrayList();
 
205,37 → 210,22
*
* @see src.plugins.PluginInterface#start
*/
public void start(Node node, Document environment, TreePath elementPath) {
 
public void start(Node node, Document environment, TreePath elementPath)
{
this.environment = environment;
oldNode = node;
changedNode = node;
gui.getStatusInterface().setIcon(StatusInterface.ICON_NONE);
gui.getStatusInterface().setMessage(null);
gui.getStatusInterface().setPoint(null);
 
printTree(node, 0);
if (node != null) {
if (node == null)
imageName = null;
else
imageName = node.getAttributes().getNamedItem("image").getNodeValue();
}
imageUrl=null; // initialised
//TreePath workingPath =elementPath;
Node root = (Node)environment.getDocumentElement();
 
 
imageUrl = getUrlForLabel(imageName,new AdapterNode(root));
if (imageUrl==null) {
gui.getLogInterface().log(LogInterface.TYPE_ERROR,"Picture "+"\""+imageName+"\""+" not found in document.");
}
 
imageNameField.setText(imageName);
loadImage(imageUrl);
findImage();
 
// Load existing regions.
initRegions(node);
244,51 → 234,64
setVisible(true);
}
 
/**
* Finds image URL and loads it.
*/
private void findImage()
{
imageUrl = findUrlForImageName(
gui.getDocumentManagerInterface().getPluginManagerInterface(),
imageName, environment.getDocumentElement());
 
if (imageUrl==null) {
gui.getLogInterface().log(LogInterface.TYPE_ERROR,
"Picture " + "\"" + imageName + "\"" + " not found in document.");
}
 
/**
* Tries to find the Url to the belonging Imagelabel among all children of 'parent'
*
* @param label: The label of the Image.
*
* @param parent: The node among its children we search.
*
* @return The belonging Url. If it can not be found null is returned
loadImage(imageUrl);
}
 
/**
* Tries to find image URL for label of the image in given node.
*
* author Matthias Donner
*
* Last revision: 23-Jul-2003
* @param manager: The plugin manager.
* @param imageName: The name of the image.
* @param node: The node.
* @return The URL or null if it can not be found
*/
private String findUrlForImageName(PluginManagerInterface manager,
String imageName, Node node)
{
// check elements only
if(!(node instanceof Element)) return null;
 
private String getUrlForLabel(String label, AdapterNode parent) {
String result;
Node temp,attribTemp;
NamedNodeMap nodeList;
boolean exit = false;
for (int i = 0; i<parent.childCount(); i++){ // we look at each child
temp=parent.getChild(i).toDomNode();
if (temp.hasAttributes()) { // if the child has attributes
nodeList=temp.getAttributes();
for (int j = 0; j< nodeList.getLength();j++){ // we look at each attribute
attribTemp=nodeList.item(j);
if (attribTemp.getNodeName().equals("name") && attribTemp.getNodeValue().equals(label)) { // if an attribute is called 'name' contains the label
for (int k = 0; k<nodeList.getLength();k++){ // we look at all attributes again and try to find image urls
String attribute =nodeList.item(k).getNodeName();
if (attribute.equals("url")) { // an image url is always called 'url' in transformed document
result = nodeList.item(k).getNodeValue();
return result; // returns the imageUrl
}
}
}
}
}
if (getUrlForLabel(label,new AdapterNode(temp))!=null) // searching in the childrens' children
return getUrlForLabel(label,new AdapterNode(temp));
}
return null; // Url not found.
String associatedPlugin = manager.getAssociatedPlugin(node.getNodeName());
 
// if this node corresponds to the image viewer - check image name
if(associatedPlugin != null && associatedPlugin.equals("ImageViewer")) {
Node transformedNode = manager.transformsNode(associatedPlugin, node);
if(transformedNode != null) {
NamedNodeMap nodeAttr = transformedNode.getAttributes();
String name = nodeAttr.getNamedItem("name").getNodeValue();
String url = nodeAttr.getNamedItem("url").getNodeValue();
 
// if the name matchs the image name we are looking for and
// the image has an URL - return the URL
if(name != null && name.equals(imageName) && url != null) return url;
}
}
 
// if nothing found - try the children
NodeList list = node.getChildNodes();
for(int i = 0; i < list.getLength(); i++) {
String url = findUrlForImageName(manager, imageName, list.item(i));
 
// if url found - return it immediately
if(url != null) return url;
}
 
// nothing found in children
return null;
}
 
/**
295,7 → 298,7
* Reads the existent regions from document.
*
* @param node: The node that the has been chosen.
*/
*/
private void initRegions(Node node)
{
regions.clear();
311,6 → 314,7
int w = 1;
int h = 1;
String name = null;
String savedNameOfNode = null;
 
for(int j = 0; j < rectChildren.getLength(); j++) {
Node subnode = rectChildren.item(j);
319,6 → 323,9
if(subnode.getNodeName().equals("COORDS")) {
NamedNodeMap attrs = subnode.getAttributes();
 
savedNameOfNode
= attrs.getNamedItem("XMLEditorSavedNameOfNode").getNodeValue();
 
try {
x = Integer.parseInt(attrs.getNamedItem("x").getNodeValue());
y = Integer.parseInt(attrs.getNamedItem("y").getNodeValue());
336,11 → 343,14
}
}
 
regions.add(new Region(name, new Rectangle(x, y, w, h)));
Region region = new Region(name, new Rectangle(x, y, w, h));
region.setSavedNameOfNode(savedNameOfNode);
regions.add(region);
}
}
}
 
// debug only !!! delete it in the release
private void printTree(Node node, int indent)
{
if(indent == 0)
396,36 → 406,33
public void setOurEventListener(OurEventListener newListener) {
ourEventListener = newListener;
}
 
/**
* Forces the plugin to report its status.
*/
*/
public void forceStatusReport() {
changedNode = oldNode.cloneNode(false);
Document tempDoc = oldNode.getOwnerDocument();
changedNode.getAttributes().getNamedItem("image").setNodeValue(imageName);
java.util.Iterator i = regions.iterator();
 
printTree(oldNode, 0);
Iterator i = regions.iterator();
while(i.hasNext()) {
Region region = (Region)i.next();
i.remove();
System.out.println(region.getRect());
Text name = tempDoc.createTextNode(region.getName());
org.w3c.dom.Element geom = tempDoc.createElement("RECTANGLE");
org.w3c.dom.Element coords = tempDoc.createElement("COORDS");
coords.setAttribute("XMLEditorSavedNameOfNode", region.getSavedNameOfNode());
coords.setAttribute("x", Integer.toString(region.getRect().x));
coords.setAttribute("y", Integer.toString(region.getRect().y));
coords.setAttribute("w", Integer.toString(region.getRect().width));
coords.setAttribute("h", Integer.toString(region.getRect().height));
/* Node nameNode = (Node)name;
Node coordsNode = (Node)coords;
Node geomNode = (Node)geom;
System.out.println("nameNode = " + nameNode);
System.out.println("coordsNode = " + coordsNode);
System.out.println("geomNode = " + geomNode);*/
geom.appendChild(name);
geom.appendChild(coords);
changedNode.appendChild((Node)geom);
}
}
printTree(changedNode, 0);
 
firePluginChangedElement(oldNode, changedNode);
}
 
434,20 → 441,11
*
* @param oldNode: ...
* @param changedNode: ..
*/
*/
private void firePluginChangedElement(Node oldNode, Node changedNode) {
OurEvent e = new OurEvent(new Object[] {oldNode, changedNode, pluginName});
ourEventListener.firedOurEvent(e);
}
/**
* ...
*
* @param region: ...
*/
private void sendRegionChanged(Region region)
{
}
 
/**
* Loads image.
504,17 → 502,19
if(e.getSource() == confirmButton) {
imageName = imageNameField.getText();
forceStatusReport();
findImage();
}
 
if(e.getSource() == regionConfirmButton) {
if(selectedRegion != null) {
selectedRegion.setName(regionNameField.getText());
sendRegionChanged(selectedRegion);
forceStatusReport();
}
}
 
if(e.getSource() == deleteSelectedItem) { // delete the selected region
deleteRegion(selectedRegion);
forceStatusReport();
}
}
 
569,9 → 569,11
 
if(x >= 0 && y >= 0 && x < image.getWidth(null) && y < image.getHeight(null)) {
inResize = true;
setSelectedRegion(new Region(null, new Rectangle(
Region region = new Region(null, new Rectangle(
(int)((e.getX() - imagePanel.getDx()) * imagePanel.getCoef()),
(int)((e.getY() - imagePanel.getDy()) * imagePanel.getCoef()), 1, 1)));
(int)((e.getY() - imagePanel.getDy()) * imagePanel.getCoef()), 1, 1));
region.setSavedNameOfNode("COORDS");
setSelectedRegion(region);
addRegion(selectedRegion);
}
}
692,8 → 694,12
showPopup(e.getX(), e.getY());
}
 
inMove = false;
inResize = false;
// if a region was moved or resized - update the document
if(inMove || inResize)
forceStatusReport();
 
inMove = false;
inResize = false;
}
}