~ubuntu-branches/ubuntu/raring/scilab/raring-proposed

« back to all changes in this revision

Viewing changes to modules/xcos/src/java/org/scilab/modules/xcos/palette/Palette.java

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2012-08-30 14:42:38 UTC
  • mfrom: (1.4.7)
  • Revision ID: package-import@ubuntu.com-20120830144238-c1y2og7dbm7m9nig
Tags: 5.4.0-beta-3-1~exp1
* New upstream release
* Update the scirenderer dep
* Get ride of libjhdf5-java dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
16
16
import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.synchronousScilabExec;
17
17
 
 
18
import java.awt.GraphicsEnvironment;
18
19
import java.awt.image.BufferedImage;
19
20
import java.io.File;
20
21
import java.io.IOException;
26
27
import javax.imageio.ImageIO;
27
28
import javax.swing.SwingUtilities;
28
29
 
29
 
import ncsa.hdf.hdf5lib.exceptions.HDF5Exception;
30
 
 
31
30
import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
32
31
import org.scilab.modules.graph.utils.ScilabExported;
33
 
import org.scilab.modules.hdf5.read.H5Read;
 
32
import org.scilab.modules.javasci.JavasciException;
 
33
import org.scilab.modules.javasci.Scilab;
34
34
import org.scilab.modules.localization.Messages;
35
35
import org.scilab.modules.types.ScilabTList;
36
36
import org.scilab.modules.xcos.Xcos;
131
131
    /**
132
132
     * Load an xcos palette into the palette manager
133
133
     *
134
 
     * @param path
135
 
     *            full path to the scilab exported palette
 
134
     * @param name
 
135
     *            the scilab exported palette variable name
136
136
     * @param category
137
137
     *            TreePath of the palette
 
138
     * @throws JavasciException
 
139
     *             on invocation error
138
140
     */
139
141
    @ScilabExported(module = XCOS, filename = PALETTE_GIWS_XML)
140
 
    public static void loadPal(final String path, final String[] category) {
 
142
    public static void loadPal(final String name, final String[] category) throws JavasciException {
 
143
        /*
 
144
         * If the env. is headless only perform fake loading to assert data
 
145
         * integrity.
 
146
         */
 
147
        if (GraphicsEnvironment.isHeadless()) {
 
148
            LOG.warning("Headless environment detected, only perform sanity check");
 
149
            loadPalHeadless(name);
 
150
            return;
 
151
        }
 
152
 
141
153
        /*
142
154
         * Import the palette
143
155
         */
144
 
        final ScilabTList data = new ScilabTList();
145
 
        final String file = new File(path).getAbsolutePath();
146
 
        try {
147
 
            final int fileId = H5Read.openFile(file);
148
 
            H5Read.readDataFromFile(fileId, data);
149
 
            H5Read.closeFile(fileId);
150
 
        } catch (final HDF5Exception e) {
151
 
            throw new RuntimeException(String.format(UNABLE_TO_IMPORT, file), e);
152
 
        }
 
156
        final ScilabTList data = (ScilabTList) Scilab.getInCurrentScilabSession(name);
153
157
 
154
158
        /*
155
159
         * handle shared data on the EDT thread
158
162
            SwingUtilities.invokeAndWait(new Runnable() {
159
163
                @Override
160
164
                public void run() {
161
 
                    /*
162
 
                     * Decode the style part of the palette
163
 
                     */
164
 
                    final mxStylesheet styleSheet = Xcos.getInstance().getStyleSheet();
165
 
                    try {
166
 
                        new StyleElement().decode(data, styleSheet);
167
 
                    } catch (final ScicosFormatException e) {
168
 
                        throw new RuntimeException(e);
169
 
                    }
170
 
 
171
 
                    // reload all the opened diagram (clear states)
172
 
                    for (final XcosDiagram d : Xcos.getInstance().openedDiagrams()) {
173
 
                        if (d != null) {
174
 
                            final mxGraphView view = d.getView();
175
 
                            if (view != null) {
176
 
                                view.reload();
177
 
                            }
178
 
 
179
 
                            final mxGraphComponent comp = d.getAsComponent();
180
 
                            if (comp != null) {
181
 
                                comp.refresh();
182
 
                            }
183
 
                        }
184
 
                    }
185
 
 
186
 
                    final PaletteNode node = getPathNode(category, true);
187
 
                    if (!(node instanceof Category)) {
188
 
                        throw new RuntimeException(String.format(WRONG_INPUT_ARGUMENT_S_INVALID_TREE_PATH, "category"));
189
 
                    }
190
 
                    final Category cat = (Category) node;
191
 
 
192
 
                    /*
193
 
                     * Adding the palette tree part of the palette
194
 
                     */
195
 
                    PreLoaded pal;
196
 
                    try {
197
 
                        pal = new PreLoadedElement().decode(data, new PreLoaded.Dynamic());
198
 
                    } catch (final ScicosFormatException e) {
199
 
                        throw new RuntimeException(e);
200
 
                    }
201
 
                    cat.getNode().add(pal);
202
 
                    pal.setParent(cat);
203
 
 
204
 
                    PaletteNode.refreshView(pal);
 
165
                    try {
 
166
                        /*
 
167
                         * Decode the style part of the palette
 
168
                         */
 
169
                        final mxStylesheet styleSheet = Xcos.getInstance().getStyleSheet();
 
170
                        try {
 
171
                            new StyleElement().decode(data, styleSheet);
 
172
                        } catch (final ScicosFormatException e) {
 
173
                            throw new RuntimeException(e);
 
174
                        }
 
175
 
 
176
                        // reload all the opened diagram (clear states)
 
177
                        for (final XcosDiagram d : Xcos.getInstance().openedDiagrams()) {
 
178
                            if (d != null) {
 
179
                                final mxGraphView view = d.getView();
 
180
                                if (view != null) {
 
181
                                    view.reload();
 
182
                                }
 
183
 
 
184
                                final mxGraphComponent comp = d.getAsComponent();
 
185
                                if (comp != null) {
 
186
                                    comp.refresh();
 
187
                                }
 
188
                            }
 
189
                        }
 
190
 
 
191
                        final PaletteNode node = getPathNode(category, true);
 
192
                        if (!(node instanceof Category)) {
 
193
                            throw new RuntimeException(String.format(WRONG_INPUT_ARGUMENT_S_INVALID_TREE_PATH, "category"));
 
194
                        }
 
195
                        final Category cat = (Category) node;
 
196
 
 
197
                        /*
 
198
                         * Adding the palette tree part of the palette
 
199
                         */
 
200
                        PreLoaded pal;
 
201
                        try {
 
202
                            pal = new PreLoadedElement().decode(data, new PreLoaded.Dynamic());
 
203
                        } catch (final ScicosFormatException e) {
 
204
                            throw new RuntimeException(e);
 
205
                        }
 
206
                        cat.getNode().add(pal);
 
207
                        pal.setParent(cat);
 
208
 
 
209
                        PaletteNode.refreshView(pal);
 
210
                    } catch (Exception e) {
 
211
                        e.printStackTrace();
 
212
                    }
205
213
                }
206
214
            });
207
215
        } catch (final InterruptedException e) {
218
226
        }
219
227
    }
220
228
 
 
229
    private static final void loadPalHeadless(final String name) throws JavasciException {
 
230
        try {
 
231
            final ScilabTList data = (ScilabTList) Scilab.getInCurrentScilabSession(name);
 
232
 
 
233
            // style check
 
234
            new StyleElement().decode(data, new mxStylesheet());
 
235
 
 
236
            // palette data check
 
237
            new PreLoadedElement().decode(data, new PreLoaded.Dynamic());
 
238
 
 
239
        } catch (ScicosFormatException e) {
 
240
            throw new RuntimeException(e);
 
241
        }
 
242
    }
 
243
 
221
244
    /**
222
245
     * Load an xcos palette into the palette manager at the root category.
223
246
     *
224
 
     * @param path
225
 
     *            full path to the scilab exported palette
 
247
     * @param name
 
248
     *            the scilab exported palette variable name
 
249
     * @throws JavasciException
 
250
     *             on invocation error
226
251
     */
227
252
    @ScilabExported(module = XCOS, filename = PALETTE_GIWS_XML)
228
 
    public static void loadPal(final String path) {
229
 
        loadPal(path, null);
 
253
    public static void loadPal(final String name) throws JavasciException {
 
254
        loadPal(name, null);
230
255
    }
231
256
 
232
257
    /**