1 /*
  2  * Created on Sep 3, 2003
  3  *
  4  * To change the template for this generated file go to
  5  * Window>Preferences>Java>Code Generation>Code and Comments
  6  */
  7 package javaclients.graphical;
  8 
  9 import java.awt.Rectangle;
 10 import java.util.StringTokenizer;
 11 
 12 import java.util.logging.Logger;
 13 import java.util.logging.Level;
 14 
 15 import org.xmlBlaster.util.Global;
 16 import org.xmlBlaster.util.XmlBlasterException;
 17 import org.xmlBlaster.util.def.ErrorCode;
 18 
 19 import CH.ifa.draw.framework.Figure;
 20 import CH.ifa.draw.framework.Drawing;
 21 import CH.ifa.draw.framework.DrawingView;
 22 import CH.ifa.draw.framework.DrawingChangeEvent;
 23 import CH.ifa.draw.framework.FigureChangeEvent;
 24 
 25 /**
 26  * MonitorCommand
 27  *
 28  * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
 29  * 
 30  */
 31 public class MonitorCommand {
 32 
 33    public final static int NONE     = 0;
 34    public final static int LOCATION = 1;
 35    public final static int SIZE     = 2;
 36    public final static int COLOR    = 4;
 37    public final static int TEXT     = 8;
 38    public final static String ME = "MonitorCommand";
 39    
 40    private Global global;
 41    private static Logger log = Logger.getLogger(MonitorCommand.class.getName());
 42    private String oid;
 43    private String type;
 44    private int    action = NONE;
 45    private Figure figure;
 46    
 47    private MonitorCommand(Global global, String oid, int action, Figure figure) {
 48       this.global = global;
 49 
 50       this.oid    = oid;
 51       this.action = action;
 52       this.figure = figure;
 53    }
 54    
 55    public MonitorCommand(Global global, String txt, Figure figure) throws XmlBlasterException {
 56       this.global = global;
 57 
 58       this.figure = figure;
 59       StringTokenizer tokenizer = new StringTokenizer(txt.trim(), ";");
 60       if (tokenizer.countTokens() != 3)
 61          throw new XmlBlasterException(this.global, ErrorCode.USER_CONFIGURATION, "The text '" + txt + "' is not recognized as a valid command");
 62       String type   = tokenizer.nextToken().trim();
 63       if ("&instance".equalsIgnoreCase(type)) {
 64          this.oid = tokenizer.nextToken().trim();
 65       }
 66       else if ("&template".equalsIgnoreCase(type)) {
 67          this.type = tokenizer.nextToken().trim();
 68       }
 69       else    
 70          throw new XmlBlasterException(this.global, ErrorCode.USER_CONFIGURATION, "The text '" + txt + "' must either start with '&instance' or '&template'");
 71 
 72       String tmp = tokenizer.nextToken().trim();
 73       if      ("location".equalsIgnoreCase(tmp)) this.action = LOCATION;
 74       else if ("size".equalsIgnoreCase(tmp)    ) this.action = SIZE;
 75       else if ("color".equalsIgnoreCase(tmp)   ) this.action = COLOR;
 76       else if ("text".equalsIgnoreCase(tmp)    ) this.action = TEXT;
 77       else throw new XmlBlasterException(this.global, ErrorCode.USER_CONFIGURATION, "The action (here) '" + tmp + "' must either be 'location', 'size', 'color' or 'text'");
 78    }
 79    
 80    public boolean isInstance() {
 81       return (this.oid != null);
 82    }
 83 
 84    public boolean isTemplate() {
 85       return (this.type != null);
 86    }
 87 
 88    public String getOid() {
 89       return this.oid;
 90    }
 91 
 92    public String getType() {
 93       return this.type;
 94    }
 95 
 96    public int getAction() {
 97       return this.action;
 98    }
 99 
100    public Figure getFigure() {
101       return this.figure;
102    }
103 
104    public void doAction(byte[] content, DrawingView view) {
105       String data = new String(content);
106       // this.figure.willChange();
107 
108       Rectangle oldRect = this.figure.displayBox();
109       try {
110          if (this.action == LOCATION) {
111             StringTokenizer tokenizer = new StringTokenizer(data.trim(), ";");
112             int x = Integer.parseInt(tokenizer.nextToken().trim());
113             int y = Integer.parseInt(tokenizer.nextToken().trim());
114             this.figure.moveBy(x-oldRect.x, y-oldRect.y);
115             
116             if (log.isLoggable(Level.FINE)) this.log.fine("new position: " + x  + " " + y);
117          }
118       }  
119       catch (Exception ex) {
120          ex.printStackTrace();         
121       }
122 
123       // this.figure.changed();
124       Rectangle newRect = this.figure.displayBox();
125       Drawing drawing = view.drawing();
126       FigureChangeEvent ev0 = new FigureChangeEvent(this.figure);
127       drawing.figureChanged(ev0);
128 
129       DrawingChangeEvent ev = new DrawingChangeEvent(drawing, oldRect);
130       view.drawingRequestUpdate(ev);
131       ev = new DrawingChangeEvent(drawing, newRect);
132       view.drawingRequestUpdate(ev);
133       // view.drawingInvalidated(ev);
134       // view.repairDamage();
135    }
136 
137    /**
138     * Creates an instance from this template or returns null if this is
139     * not a template
140     * @param oid the oid to give to the new instance
141     * @return
142     */
143    public MonitorCommand createInstance(String oid) {
144       if (!this.isTemplate()) return null;
145       Figure fig = (Figure)this.figure.clone();
146       return new MonitorCommand(this.global, oid, this.action, fig);
147    }
148 
149    public void remove(DrawingView view) {
150       Drawing drawing = view.drawing();
151       drawing.remove(this.figure);
152       drawing.orphan(this.figure);
153       FigureChangeEvent ev = new FigureChangeEvent(this.figure);
154       drawing.figureRequestUpdate(ev);
155    }
156 
157 
158    public static void main(String[] args) {
159       String txt = "&instance;plane.110;LOCATION";
160 
161       try {
162          MonitorCommand command = new MonitorCommand(new Global(), txt, null);
163          System.out.println("is instance: " + command.isInstance());
164          System.out.println("is template: " + command.isTemplate());
165 
166          if (command.isInstance()) 
167             System.out.println("oid: " + command.getOid());
168          if (command.isTemplate()) 
169             System.out.println("type: " + command.getType());
170 
171          System.out.println("action: " + command.getAction());
172          
173       }
174       
175       catch (Exception ex) {
176          System.err.println(ex.getMessage());         
177       }
178       
179    }
180 }


syntax highlighted by Code2HTML, v. 0.9.1