Xcos: remove block default values
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / palette / PaletteBlockCtrl.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - ClĂ©ment DAVID
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.xcos.palette;
14
15 import java.awt.Point;
16 import java.awt.datatransfer.Transferable;
17 import java.awt.dnd.DnDConstants;
18 import java.awt.dnd.DragGestureEvent;
19 import java.awt.dnd.DragGestureListener;
20 import java.awt.dnd.DragSource;
21 import java.awt.dnd.InvalidDnDOperationException;
22 import java.awt.event.MouseListener;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.scilab.modules.gui.messagebox.ScilabModalDialog;
27 import org.scilab.modules.gui.messagebox.ScilabModalDialog.IconType;
28 import org.scilab.modules.localization.Messages;
29 import org.scilab.modules.xcos.block.BasicBlock;
30 import org.scilab.modules.xcos.block.BlockFactory;
31 import org.scilab.modules.xcos.block.BlockFactory.BlockInterFunction;
32 import org.scilab.modules.xcos.graph.XcosDiagram;
33 import org.scilab.modules.xcos.io.scicos.H5RWHandler;
34 import org.scilab.modules.xcos.palette.listener.PaletteBlockMouseListener;
35 import org.scilab.modules.xcos.palette.model.PaletteBlock;
36 import org.scilab.modules.xcos.palette.view.PaletteBlockView;
37 import org.scilab.modules.xcos.palette.view.PaletteManagerView;
38 import org.scilab.modules.xcos.utils.XcosMessages;
39
40 import com.mxgraph.swing.handler.mxGraphTransferHandler;
41 import com.mxgraph.util.mxConstants;
42
43 /**
44  * A palette block is the representation of the block in the palette. All the
45  * operations there are used to render, load and put (on a diagram) a block.
46  */
47 public final class PaletteBlockCtrl {
48
49         private static final MouseListener MOUSE_LISTENER = new PaletteBlockMouseListener();
50         private static final Log LOG = LogFactory.getLog(PaletteBlockCtrl.class);
51         
52         private static final String UNABLE_TO_LOAD_BLOCK = Messages.gettext("Unable to load block from %s .");
53         private static final String LOADING_THE_BLOCK = Messages.gettext("Loading the block") + XcosMessages.DOTS;
54         
55         /**
56          * Internal graph used to render each block.
57          */
58         public static final XcosDiagram INTERNAL_GRAPH;
59         static {
60                 INTERNAL_GRAPH = new XcosDiagram();
61                 INTERNAL_GRAPH.installListeners();
62         }
63         
64         private static PaletteBlockCtrl previouslySelected;
65         
66         private final PaletteBlock model;
67         private final PaletteBlockView view;
68         
69         private Transferable transferable;
70         
71         /**
72          * Default constructor
73          * @param model the block data
74          */
75         public PaletteBlockCtrl(PaletteBlock model) {
76                 this.model = model;
77                 this.view = new PaletteBlockView(this);
78                 installListeners(this.view);
79         }
80         
81         /**
82          * @param view The view to setup
83          */
84         private void installListeners(PaletteBlockView view) {
85                 view.addMouseListener(MOUSE_LISTENER);
86                 installDnd();
87         }
88
89         /**
90          * @return the view
91          */
92         public PaletteBlockView getView() {
93                 return view;
94         }
95         
96         /**
97          * @return the model
98          */
99         public PaletteBlock getModel() {
100                 return model;
101         }
102         
103         
104         /**
105          * This function is the only access to get the block.
106          * @return the transferable object
107          */
108         public Transferable getTransferable() {
109                 if (transferable == null) {
110                         /* Load the block from the H5 file */
111                         BasicBlock block = loadBlock();
112                         if (block == null) {
113                                 if (LOG.isInfoEnabled()) {
114                                         LOG.info(String.format(UNABLE_TO_LOAD_BLOCK,
115                                                         getModel().getData().getEvaluatedPath()));
116                                 }
117                                 getView().setEnabled(false);
118                                 return null;
119                         }
120                         getView().setEnabled(true);
121                         
122                         /* Render it and export it */
123                         INTERNAL_GRAPH.addCell(block);
124                         INTERNAL_GRAPH.selectAll();
125                         
126                         INTERNAL_GRAPH.updateCellSize(block);
127                         
128                         mxGraphTransferHandler handler = ((mxGraphTransferHandler) INTERNAL_GRAPH
129                                         .getAsComponent().getTransferHandler());
130                         transferable = handler.createTransferable(INTERNAL_GRAPH.getAsComponent());
131                         
132                         INTERNAL_GRAPH.removeCells();
133                 }
134                 return transferable;
135         }
136
137         /**
138          * @return the loaded block.
139          */
140         public BasicBlock loadBlock() {
141                 BasicBlock block;
142                 if (model.getName().compareTo("TEXT_f") != 0) {
143                         // Load the block from the file
144                         String realPath = model.getData().getEvaluatedPath(); 
145                         block = new H5RWHandler(realPath).readBlock();
146                         
147                         // invalid block case
148                         if (block == null) {
149                                 return null;
150                         }
151                         
152                         if (block.getStyle().compareTo("") == 0) {
153                                 block.setStyle(block.getInterfaceFunctionName());
154                         }
155                 } else {
156                         block = BlockFactory.createBlock(BlockInterFunction.TEXT_f);
157                 }
158                 return block;
159         }
160         
161         /**
162          * @return true if it is selected, false otherwise
163          */
164         public boolean isSelected() {
165                 return this == previouslySelected;
166         }
167
168         /**
169          * @param selected the selected state to set
170          */
171         public void setSelected(boolean selected) {
172                 if (selected) {
173                         if (previouslySelected != null) {
174                                 previouslySelected.setSelected(false);
175                         }
176                         previouslySelected = this;
177                 }
178                 getView().setSelectedUI(selected);
179         }
180
181         /**
182          * Install the Drag'n'Drop on this instance.
183          */
184         public void installDnd() {
185                 // Install the handler for dragging nodes into a graph
186                 DragGestureListener dragGestureListener = new DragGestureListener() {
187                         public void dragGestureRecognized(DragGestureEvent e) {
188                                 final PaletteManagerView winView = PaletteManager.getInstance()
189                                                 .getView();
190                                 final DragGestureEvent event = e;
191                                 final String msg = String.format(UNABLE_TO_LOAD_BLOCK,
192                                                 getModel().getData().getEvaluatedPath());
193
194                                 winView.setInfo(LOADING_THE_BLOCK);
195                                 try {
196                                         Transferable transfer = getTransferable();
197
198                                         if (transfer != null) {
199                                                 event.startDrag(null, mxConstants.EMPTY_IMAGE, new Point(),
200                                                                 transfer, null);
201                                         } else {
202                                                 throw new NullPointerException();
203                                         }
204                                 } catch (InvalidDnDOperationException exception) {
205                                         ScilabModalDialog.show(winView, msg,
206                                                         XcosMessages.XCOS_ERROR, IconType.ERROR_ICON);
207                                 } finally {
208                                         winView.setInfo(XcosMessages.EMPTY_INFO);
209                                 }
210                         }
211
212                 };
213
214                 DragSource dragSource = DragSource.getDefaultDragSource();
215                 dragSource.createDefaultDragGestureRecognizer(this.getView(),
216                                 DnDConstants.ACTION_COPY, dragGestureListener);
217         }
218
219 }