View Javadoc

1   /*
2    *  nextobjects Copyright (C) 2001-2005 Emmanuel Florent
3    *  This program is free software; you can redistribute it and/or modify
4    *  it under the terms of the GNU General Public License as published by the
5    *  Free Software Foundation; either version 2 of the License, or (at your
6    *  option) any later version.
7    *  This program is distributed in the hope that it will
8    *  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
9    *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10   *  PURPOSE. See the GNU General Public License for more details.
11   *  You should have received a copy of the GNU General Public License along
12   *  with this program; if not, write to the Free Software Foundation, Inc., 59
13   *  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14   */
15  package org.devaki.nextobjects.util;
16  
17  import java.io.IOException;
18  import java.util.Vector;
19  import java.awt.Point;
20  import java.awt.datatransfer.Clipboard;
21  import java.awt.datatransfer.ClipboardOwner;
22  import java.awt.datatransfer.Transferable;
23  import java.awt.datatransfer.UnsupportedFlavorException;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.devaki.nextobjects.ui.main.NOTreeView;
27  import org.devaki.nextobjects.workspace.models.ConceptualModel;
28  import org.devaki.nextobjects.workspace.models.PhysicalModel;
29  import org.devaki.nextobjects.workspace.models.columns.Column;
30  import org.devaki.nextobjects.workspace.models.graphics.ClassView;
31  import org.devaki.nextobjects.workspace.models.graphics.LineView;
32  import org.devaki.nextobjects.workspace.models.graphics.ObjectSelection;
33  import org.devaki.nextobjects.workspace.models.objects.Association;
34  import org.devaki.nextobjects.workspace.models.objects.AssociationLink;
35  import org.devaki.nextobjects.workspace.models.objects.BaseObject;
36  import org.devaki.nextobjects.workspace.models.objects.BaseClass;
37  import org.devaki.nextobjects.workspace.models.objects.Constraint;
38  import org.devaki.nextobjects.workspace.models.objects.Entity;
39  import org.devaki.nextobjects.workspace.models.objects.InheritanceLink;
40  import org.devaki.nextobjects.workspace.models.objects.Table;
41  import org.devaki.nextobjects.workspace.models.objects.Label;
42  import org.devaki.nextobjects.workspace.models.styles.ClassStyle;
43  import org.devaki.nextobjects.workspace.models.styles.LineStyle;
44  
45  /***
46   *  Tha App Clipboard Based on simple examples found on www 
47   * @todo: improve/simplify clippedstyle maybe also style data storing of theirs style
48   *
49   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
50   * @created    December 27, 2003
51   */
52  public class NOClipboard implements ClipboardOwner
53  {
54      /***  The javasource clipboard object */
55      private static Clipboard clipboard = new Clipboard("NextObjects Clipboard");
56  
57      /***  Local reference */
58      private static NOClipboard myClipboard;
59  
60      /***  The log4j logger */
61      private static Log logger =
62          LogFactory.getLog(NOClipboard.class.getName());
63  
64  
65      /***  Construct the clipboard */
66      public NOClipboard()
67      {
68          myClipboard = this;
69      }
70  
71  
72      /***
73       *  This function must be declared because of the class implementation
74       *
75       * @param  parClipboard     the parent clipboard
76       * @param  parTransferable  the parent transferable object
77       */
78      public void lostOwnership(Clipboard parClipboard,
79          Transferable parTransferable)
80      {
81      }
82  
83  
84      /***
85       *  Define if there is a usable class like object in the clipboard.
86       *
87       * @return    true if a BaseObject is in the clipboard
88       */
89      public static boolean isBaseObjectClipped()
90      {
91      Transferable clipboardContent = clipboard.getContents(myClipboard);
92  
93          return (
94              (clipboardContent != null)
95               && (clipboardContent.isDataFlavorSupported(
96              ObjectSelection.baseObjectFlavor)));
97      }
98  
99  
100     /***
101      *  Define if there is a column like object in the clipboard
102      *
103      * @return    true if there is a column in the clipboard
104      */
105     public static boolean isColumnClipped()
106     {
107     Transferable clipboardContent = clipboard.getContents(myClipboard);
108 
109         return (
110             (clipboardContent != null)
111              && (clipboardContent.isDataFlavorSupported(Column.getFieldFlavor())));
112     }
113 
114 
115     /***
116      *  Define if there is a style object in the clipboard
117      *
118      * @return    true if there is a ClassStyle object in the clipboard
119      */
120     public static boolean isClassStyleClipped()
121     {
122     Transferable clipboardContent = clipboard.getContents(myClipboard);
123 
124         return (
125             (clipboardContent != null)
126              && (clipboardContent.isDataFlavorSupported(
127             ClassStyle.fieldFlavor)));
128     }
129 
130 
131     /***
132      *  Define if there is a style object in the clipboard
133      *
134      * @return    true if there is a LineStyle object in the clipboard
135      */
136     public static boolean isLineStyleClipped()
137     {
138     Transferable clipboardContent = clipboard.getContents(myClipboard);
139 
140         return (
141             (clipboardContent != null)
142              && (clipboardContent.isDataFlavorSupported(
143             LineStyle.fieldFlavor)));
144     }
145 
146 
147     /***
148      *  Cut an object
149      *
150      * @param  objects  a Vector containing the objects to cut
151      */
152     public static void cut(Vector objects)
153     {
154         copy(objects);
155         ModelMan.delete();
156     }
157 
158 
159     /***
160      *  Copy an object
161      *
162      * @param  pCurrentObjects  the objects to be copied
163      */
164     public static void copy(Vector pCurrentObjects)
165     {
166     Vector v = new Vector(pCurrentObjects.size());
167 
168         for (int i = 0; i < pCurrentObjects.size(); i++)
169         {
170             // Conceptual Models Objects
171             // maybe can be improved.
172             if (pCurrentObjects.elementAt(i) instanceof InheritanceLink)
173             {
174             InheritanceLink tObject = new InheritanceLink(
175                     (InheritanceLink) pCurrentObjects.elementAt(i));
176 
177                 v.addElement(tObject);
178             }
179             if (pCurrentObjects.elementAt(i) instanceof Entity)
180             {
181             Entity tObject = new Entity((Entity) pCurrentObjects.elementAt(i));
182 
183                 v.addElement(tObject);
184             }
185             if (pCurrentObjects.elementAt(i) instanceof Association)
186             {
187             Association tObject = new Association(
188                     (Association) pCurrentObjects.elementAt(i));
189 
190                 v.addElement(tObject);
191             }
192             if (pCurrentObjects.elementAt(i) instanceof AssociationLink)
193             {
194             AssociationLink tObject = new AssociationLink(
195                     (AssociationLink) pCurrentObjects.elementAt(i));
196 
197                 v.addElement(tObject);
198             }
199 
200             // Physical Model Objects
201             if (pCurrentObjects.elementAt(i) instanceof Table)
202             {
203             Table tObject = new Table((Table) pCurrentObjects.elementAt(i));
204 
205                 v.addElement(tObject);
206             }
207             if (pCurrentObjects.elementAt(i) instanceof Constraint)
208             {
209             Constraint tObject = new Constraint(
210                     (Constraint) pCurrentObjects.elementAt(i));
211 
212                 v.addElement(tObject);
213             }
214             if (pCurrentObjects.elementAt(i) instanceof Label)
215             {
216             Label tObject = new Label((Label) pCurrentObjects.elementAt(i));
217 
218                 v.addElement(tObject);
219             }
220         }
221 
222     ObjectSelection contents = new ObjectSelection(v);
223 
224         clipboard.setContents(contents, myClipboard);
225     }
226 
227 
228     /***
229      *  Copy some Text
230      *
231      * @param  pString  the string to be copied
232      */
233     public static void copy(String pString)
234     {
235     ObjectSelection contents = new ObjectSelection((String) pString);
236 
237         clipboard.setContents(contents, myClipboard);
238     }
239 
240 
241     /***
242      *  Copy a class style
243      *
244      * @param  pClassStyle  the class style to be copied
245      */
246     public static void copy(ClassStyle pClassStyle)
247     {
248     ClassStyle contents = new ClassStyle((ClassStyle) pClassStyle);
249 
250         clipboard.setContents(contents, myClipboard);
251     }
252 
253 
254     /***
255      *  Copy a line style
256      *
257      * @param  pLineStyle  the line style to be copied
258      */
259     public static void copy(LineStyle pLineStyle)
260     {
261     LineStyle contents = new LineStyle((LineStyle) pLineStyle);
262 
263         clipboard.setContents(contents, myClipboard);
264     }
265 
266 
267     /***
268      *  Paste the objects contained in the clipboard TODO: simplify using
269      *  BaseModel instead of ConceptualModel/PhysicalModel
270      *
271      * @param  x  the x coordinate
272      * @param  y  the y coordinate
273      */
274     public static void pasteBaseObject(int x, int y)
275     {
276     Transferable clipboardContent = clipboard.getContents(myClipboard);
277 
278         try
279         {
280         Vector objects = new Vector((Vector) clipboardContent.getTransferData(
281                 ObjectSelection.baseObjectFlavor));
282 
283             for (int i = 0; i < objects.size(); i++)
284             {
285             BaseObject clippedObject = (BaseObject) objects.elementAt(i);
286 
287                 x = x + (10 * i);
288                 y = y + (10 * i);
289 
290                 if (clippedObject instanceof InheritanceLink)
291                 {
292                 InheritanceLink newObject = new InheritanceLink(
293                         (InheritanceLink) clippedObject);
294 
295                     ModelMan.addInheritanceLink(
296                         ((ConceptualModel) ModelMan.getCurrentModel()),
297                         newObject);
298                     newObject.getObjectView().setLocation(new Point(x, y));
299                     ModelMan.addCurrentObject(newObject);
300                 }
301 
302                 if (ModelMan.getCurrentModel() instanceof ConceptualModel)
303                 {
304                     if (clippedObject instanceof Entity)
305                     {
306                     Entity newObject = new Entity((Entity) clippedObject);
307 
308                         newObject.getObjectView().setLocation(new Point(x, y));
309                         ModelMan.addEntity(
310                             ((ConceptualModel) ModelMan.getCurrentModel()),
311                             newObject);
312                         ModelMan.addCurrentObject(newObject);
313                     }
314                     else if (clippedObject instanceof Association)
315                     {
316                     Association newObject = new Association(
317                             (Association) clippedObject);
318 
319                         ModelMan.addAssociation(
320                             ((ConceptualModel) ModelMan.getCurrentModel()),
321                             newObject);
322                         newObject.getObjectView().setLocation(new Point(x, y));
323                         ModelMan.addCurrentObject(newObject);
324                     }
325                     else if (clippedObject instanceof AssociationLink)
326                     {
327                     AssociationLink newObject = new AssociationLink(
328                             (AssociationLink) clippedObject);
329 
330                         ModelMan.addAssociationLink(
331                             ((ConceptualModel) ModelMan.getCurrentModel()),
332                             newObject);
333                         newObject.getObjectView().setLocation(new Point(x, y));
334                         ModelMan.addCurrentObject(newObject);
335                     }
336                     else if (clippedObject instanceof Label)
337                     {
338                     Label newObject = new Label((Label) clippedObject);
339 
340                         ModelMan.addLabel(
341                             ModelMan.getCurrentModel(), newObject);
342                         newObject.getObjectView().setLocation(new Point(x, y));
343                         ModelMan.addCurrentObject(newObject);
344                     }
345 
346                 }
347                 else if (ModelMan.getCurrentModel() instanceof PhysicalModel)
348                 {
349                     if (clippedObject instanceof Table)
350                     {
351                     Table newObject = new Table((Table) clippedObject);
352 
353                         ModelMan.addTable(
354                             ((PhysicalModel) ModelMan.getCurrentModel()),
355                             newObject);
356                         newObject.getObjectView().setLocation(new Point(x, y));
357                         ModelMan.addCurrentObject(newObject);
358                     }
359                     else if (clippedObject instanceof Constraint)
360                     {
361                     Constraint newObject =
362                             new Constraint((Constraint) clippedObject);
363 
364                         ModelMan.addConstraint(
365                             ((PhysicalModel) ModelMan.getCurrentModel()),
366                             newObject);
367                         ModelMan.addCurrentObject(newObject);
368                     }
369                 }
370                 else if (clippedObject instanceof Label)
371                 {
372                 Label newObject = new Label((Label) clippedObject);
373 
374                     ModelMan.addLabel(
375                         ((PhysicalModel) ModelMan.getCurrentModel()),
376                         newObject);
377                     newObject.getObjectView().setLocation(new Point(x, y));
378                     ModelMan.addCurrentObject(newObject);
379                 }
380             }
381         }
382         catch (IOException ioe)
383         {
384             logger.error("Cannot get transfer data" + ioe);
385         }
386         catch (UnsupportedFlavorException ufe)
387         {
388             logger.error("Cannot get transfer data" + ufe);
389         }
390         ModelMan.getCurrentModel().getModelView().repaint();
391     }
392 
393 
394     /***
395      *  Paste a field contained in the clipboard
396      *
397      * @param  object  the column to paste
398      */
399     public static void pasteColumn(BaseObject object)
400     {
401     Transferable clipboardContent = clipboard.getContents(myClipboard);
402 
403         try
404         {
405         Column clippedObject = (Column) clipboardContent.getTransferData(
406                 Column.getFieldFlavor());
407 
408             clippedObject.setParent((BaseClass) object);
409             // Add element to the given object
410             object.getData().addElement(clippedObject);
411             // Reload tree from the parent node
412             NOTreeView.reloadBaseClassChildrens(
413                 clippedObject.getParent().getDynamicTreeNode());
414             // Repaint associate model only if it is the current model
415             if (clippedObject.getParent().getMyModel().getPanel().isShowing())
416             {
417                 ModelMan.getCurrentModel().getPanel().repaint();
418             }
419         }
420         catch (IOException ioe)
421         {
422             logger.error("Cannot get transfer data" + ioe);
423         }
424         catch (UnsupportedFlavorException ufe)
425         {
426             logger.error("Cannot get transfer data" + ufe);
427         }
428     }
429 
430 
431     /***
432      *  Paste clipped style
433      *
434      * @param  pClassView  the class view to which the style will apply
435      */
436     public static void pasteClassStyle(ClassView pClassView)
437     {
438     Transferable clipboardContent = clipboard.getContents(myClipboard);
439 
440         try
441         {
442         ClassStyle clippedObject =
443                 (ClassStyle) clipboardContent.getTransferData(
444                 ClassStyle.fieldFlavor);
445 
446             pClassView.setStyle(clippedObject);
447         }
448         catch (IOException ioe)
449         {
450             logger.error("Cannot get transfer data" + ioe);
451         }
452         catch (UnsupportedFlavorException ufe)
453         {
454             logger.error("Cannot get transfer data" + ufe);
455         }
456     }
457 
458 
459     /***
460      *  Paste clipped style
461      *
462      * @param  pLineView  the line view to which the style will apply
463      */
464     public static void pasteLineStyle(LineView pLineView)
465     {
466     Transferable clipboardContent = clipboard.getContents(myClipboard);
467 
468         try
469         {
470         LineStyle clippedObject = (LineStyle) clipboardContent.getTransferData(
471                 LineStyle.fieldFlavor);
472 
473             pLineView.setStyle(clippedObject);
474         }
475         catch (IOException ioe)
476         {
477             logger.error("Cannot get transfer data" + ioe);
478         }
479         catch (UnsupportedFlavorException ufe)
480         {
481             logger.error("Cannot get transfer data" + ufe);
482         }
483     }
484 }
485