2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Antoine ELIAS
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
13 package org.scilab.modules.xcos.graph;
17 import org.scilab.modules.xcos.block.BasicBlock;
18 import org.scilab.modules.xcos.block.SplitBlock;
19 import org.scilab.modules.xcos.block.TextBlock;
20 import org.scilab.modules.xcos.link.BasicLink;
21 import org.scilab.modules.xcos.utils.BlockPositioning;
22 import org.scilab.modules.xcos.utils.ConfigXcosManager;
23 import org.scilab.modules.xcos.utils.XcosConstants;
25 import com.mxgraph.model.mxGeometry;
28 public class PaletteDiagram extends XcosDiagram {
30 private static int BLOCK_MAX_WIDTH = (int) (XcosConstants.PALETTE_BLOCK_WIDTH * 0.8); //80% of the max size
31 private static int BLOCK_MAX_HEIGHT = (int) (XcosConstants.PALETTE_BLOCK_HEIGHT * 0.8); //80% of the max size
33 private String fileName;
34 private double windowWidth = 0;
36 public PaletteDiagram() {
40 setGridVisible(false);
41 setCellsDeletable(false);
42 setCellsEditable(false);
44 undoManager.setEventsEnabled(false);
47 public boolean openDiagramAsPal(String diagramFileName) {
48 File theFile = new File(diagramFileName);
50 //int windowHeight = getAsComponent().getHeight();
52 if (theFile.exists()) {
53 boolean loaded = transformAndLoadFile(theFile, true);
57 setName(theFile.getName());
58 setFileName(theFile.getAbsolutePath());
59 getRubberBand().setEnabled(false);
61 /*change some diagram parameters*/
63 for(int i = 0 ; i < getModel().getChildCount(getDefaultParent()) ; i++) {
64 Object obj = getModel().getChildAt(getDefaultParent(), i);
65 if(obj instanceof BasicLink || obj instanceof SplitBlock || obj instanceof TextBlock) {
66 getModel().remove(obj);
70 ConfigXcosManager.saveUserDefinedPalettes(diagramFileName);
76 public void updateDiagram(double newWidth) {
78 if(newWidth == windowWidth) {
82 int oldRowItem = (int) (newWidth / (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN));
83 int maxRowItem = (int) (windowWidth / (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN));
85 //only compute for signifiant changes
86 if(oldRowItem == maxRowItem) {
90 windowWidth = newWidth;
93 getModel().beginUpdate();
94 for(int i = 0 ; i < getModel().getChildCount(getDefaultParent()) ; i++) {
95 Object obj = getModel().getChildAt(getDefaultParent(), i);
96 if(obj instanceof BasicBlock){
97 BasicBlock block = (BasicBlock)obj;
98 block.setGeometry(getNewBlockPosition(block.getGeometry(), blockCount));
99 BlockPositioning.updateBlockView(block);
103 getModel().endUpdate();
108 private mxGeometry getNewBlockPosition(mxGeometry geom, int blockCount) {
110 int maxRowItem = (int) (windowWidth / (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN));
111 int row = blockCount % maxRowItem;
112 int col = blockCount / maxRowItem;
113 double x = geom.getX();
114 double y = geom.getY();
115 double w = geom.getWidth();
116 double h = geom.getHeight();
118 if(geom.getWidth() > BLOCK_MAX_WIDTH || geom.getHeight() > BLOCK_MAX_HEIGHT) {
119 //update block size to fill "block area"
120 double ratio = Math.min(BLOCK_MAX_HEIGHT / h, BLOCK_MAX_WIDTH / w);
125 x = row * (XcosConstants.PALETTE_BLOCK_WIDTH + XcosConstants.PALETTE_HMARGIN);
126 x += (XcosConstants.PALETTE_BLOCK_WIDTH - w) / 2;
127 y = col * (XcosConstants.PALETTE_BLOCK_HEIGHT + XcosConstants.PALETTE_VMARGIN);
128 y += (XcosConstants.PALETTE_BLOCK_HEIGHT - h) / 2;
130 return new mxGeometry(x,y,w,h);
133 public String getName() {
137 public void setName(String name) {
141 public boolean isCellConnectable(Object cell) {
145 public void setFileName(String fileName) {
146 this.fileName = fileName;
149 public String getFileName() {