package svgdevicestest.maps;
|
|
import art.library.gui.FlatGUI;
|
import art.library.gui.flat.FlatButton;
|
import art.library.gui.flat.FlatMenu;
|
import art.library.gui.flat.FlatMenuItemCheck;
|
import art.library.gui.flat.FlatPopupMenu;
|
import art.library.interop.serialization.Serialization;
|
import art.library.utils.ArticReflection;
|
import art.library.utils.ArticScalableVectorGraphics;
|
import art.library.utils.resources.Resources;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.kitfox.svg.SVGDiagram;
|
import com.kitfox.svg.SVGElement;
|
import com.kitfox.svg.SVGUniverse;
|
import com.kitfox.svg.Text;
|
import com.kitfox.svg.animation.AnimationElement;
|
import java.awt.AlphaComposite;
|
import java.awt.Color;
|
import java.awt.Component;
|
import java.awt.Dimension;
|
import java.awt.Graphics;
|
import java.awt.Graphics2D;
|
import java.awt.MouseInfo;
|
import java.awt.Point;
|
import java.awt.Rectangle;
|
import java.awt.RenderingHints;
|
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionListener;
|
import java.awt.event.ComponentAdapter;
|
import java.awt.event.ComponentEvent;
|
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseMotionAdapter;
|
import java.awt.event.MouseWheelEvent;
|
import java.awt.event.MouseWheelListener;
|
import java.awt.geom.Point2D;
|
import java.awt.geom.Rectangle2D;
|
import java.io.ByteArrayInputStream;
|
import java.io.File;
|
import java.nio.file.Files;
|
import java.util.ArrayList;
|
import java.util.List;
|
import javax.swing.JButton;
|
import javax.swing.SwingConstants;
|
import javax.swing.SwingUtilities;
|
import javax.swing.Timer;
|
|
public class PanelMap extends JButton
|
{
|
|
private SVGDiagram diagram = null;
|
private FlatButton buttonMenu = null;
|
private FlatButton buttonTop = null;
|
private FlatButton buttonLeft = null;
|
private FlatButton buttonBottom = null;
|
private FlatButton buttonRight = null;
|
private FlatButton buttonCenter = null;
|
private FlatButton buttonMinus = null;
|
private FlatButton buttonPlus = null;
|
|
private boolean lockNavigation = false;
|
private boolean fitToPage = true;
|
private double scalex = 1.0;
|
private double scaley = 1.0;
|
private boolean dragging = false;
|
private double translatex = 0.0;
|
private double translatey = 0.0;
|
private double viewportx = 0;
|
private double viewporty = 0;
|
private double worldx = 0;
|
private double worldy = 0;
|
private double worlddraggingx = 0;
|
private double worlddraggingy = 0;
|
private Object selectedObject = null;
|
private List<String> llayer = new ArrayList<String>();
|
private List<String> ldevice = new ArrayList<String>();
|
private Timer timer = null;
|
|
public PanelMap()
|
{
|
initialise();
|
events();
|
}
|
|
public PanelMap(File file)
|
{
|
reload(file);
|
initialise();
|
events();
|
}
|
|
public PanelMap(byte[] data)
|
{
|
reload(data);
|
initialise();
|
events();
|
}
|
|
public boolean isValid()
|
{
|
return (diagram != null);
|
}
|
|
public void reload(File file)
|
{
|
try
|
{
|
reload(Files.readAllBytes(file.toPath()));
|
}
|
catch (Exception e)
|
{
|
}
|
|
this.repaint();
|
}
|
|
public void reload(byte[] data)
|
{
|
this.diagram = null;
|
|
try
|
{
|
try
|
{
|
ByteArrayInputStream bis = new ByteArrayInputStream(data);
|
SVGUniverse svgUniverse = new SVGUniverse();
|
svgUniverse.setVerbose(false);
|
diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(bis, "diagram", true));
|
llayer.clear();
|
|
for (int i = 0; i < diagram.getRoot().getNumChildren(); i++)
|
{
|
try
|
{
|
SVGElement child = diagram.getRoot().getChild(i);
|
String label = child.getPresAbsolute("inkscape:label").getStringValue();
|
String groupMode = child.getPresAbsolute("inkscape:groupmode").getStringValue();
|
if (groupMode.equalsIgnoreCase("layer") == true)
|
{
|
String display = ArticScalableVectorGraphics.getAttributeString(child, "display");
|
|
if (display == null)
|
{
|
child.addAttribute("display", AnimationElement.AT_CSS, "inline");
|
child.addAttribute("art.display", AnimationElement.AT_CSS, "inline");
|
display = ArticScalableVectorGraphics.getAttributeString(child, "display");
|
}
|
|
if (display != null)
|
{
|
if (display.equalsIgnoreCase("none") == true)
|
{
|
// SVG Salamander bug. If layer is display:none we can to change all objects
|
// recursively.
|
// If not only first object of the layer is not displayed
|
|
setAttribute(child, "display", AnimationElement.AT_CSS, "none", 99999);
|
child.addAttribute("art.display", AnimationElement.AT_CSS, "none");
|
}
|
else
|
{
|
child.addAttribute("art.display", AnimationElement.AT_CSS, "inline");
|
}
|
}
|
|
llayer.add(label);
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
diagram = null;
|
}
|
|
}
|
catch (Exception e)
|
{
|
}
|
|
this.fit();
|
}
|
|
public void paint(Graphics graphics)
|
{
|
try
|
{
|
Graphics2D g2 = (Graphics2D) graphics;
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
|
g2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DEFAULT);
|
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
|
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
|
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
|
g2.setBackground(Color.white);
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
g2.clearRect(0, 0, getWidth(), getHeight());
|
|
g2.scale(scalex, scaley);
|
g2.translate(translatex, translatey);
|
diagram.render(g2);
|
g2.translate(-translatex, -translatey);
|
g2.scale(1 / scalex, 1 / scaley);
|
|
g2.scale(1.0, 1.0);
|
g2.translate(0, 0);
|
g2.setColor(Color.red);
|
|
if (isFocusOwner() == true)
|
{
|
paintMenu(g2);
|
paintPanTiltZoom(g2);
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
private void initialise()
|
{
|
buttonMenu = new FlatButton();
|
{
|
buttonMenu.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/menu.png"));
|
buttonMenu.setPreferredSize(new Dimension(buttonMenu.getStringWidth(), 0));
|
buttonMenu.setHorizontalAlignment(SwingConstants.CENTER);
|
buttonMenu.setHorizontalTextPosition(SwingConstants.CENTER);
|
buttonMenu.getLook().focus = false;
|
buttonMenu.getLook().borderRollover = false;
|
buttonMenu.getLook().borderSelected = false;
|
buttonMenu.getLook().colorBackgroundSelected = null;
|
buttonMenu.getLook().colorBackgroundRollover = null;
|
buttonMenu.revalidate();
|
buttonMenu.addActionListener(new ActionListener()
|
{
|
public void actionPerformed(ActionEvent e)
|
{
|
showMenu(0, 0, true);
|
repaint();
|
}
|
});
|
}
|
|
buttonTop = new FlatButton();
|
{
|
buttonTop.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/move-top.png"));
|
buttonTop.setPreferredSize(new Dimension(buttonTop.getStringWidth(), 0));
|
buttonTop.setHorizontalAlignment(SwingConstants.CENTER);
|
buttonTop.setHorizontalTextPosition(SwingConstants.CENTER);
|
buttonTop.getLook().focus = false;
|
buttonTop.getLook().borderRollover = false;
|
buttonTop.getLook().borderSelected = false;
|
buttonTop.getLook().colorBackgroundSelected = null;
|
buttonTop.getLook().colorBackgroundRollover = null;
|
buttonTop.revalidate();
|
}
|
|
buttonLeft = new FlatButton();
|
{
|
buttonLeft.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/move-left.png"));
|
buttonLeft.setPreferredSize(new Dimension(buttonLeft.getStringWidth(), 0));
|
buttonLeft.setHorizontalAlignment(SwingConstants.CENTER);
|
buttonLeft.setHorizontalTextPosition(SwingConstants.CENTER);
|
buttonLeft.getLook().focus = false;
|
buttonLeft.getLook().borderRollover = false;
|
buttonLeft.getLook().borderSelected = false;
|
buttonLeft.getLook().colorBackgroundSelected = null;
|
buttonLeft.getLook().colorBackgroundRollover = null;
|
buttonLeft.revalidate();
|
}
|
|
buttonBottom = new FlatButton();
|
{
|
buttonBottom.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/move-bottom.png"));
|
buttonBottom.setPreferredSize(new Dimension(buttonBottom.getStringWidth(), 0));
|
buttonBottom.setHorizontalAlignment(SwingConstants.CENTER);
|
buttonBottom.setHorizontalTextPosition(SwingConstants.CENTER);
|
buttonBottom.getLook().focus = false;
|
buttonBottom.getLook().borderRollover = false;
|
buttonBottom.getLook().borderSelected = false;
|
buttonBottom.getLook().colorBackgroundSelected = null;
|
buttonBottom.getLook().colorBackgroundRollover = null;
|
buttonBottom.revalidate();
|
}
|
|
buttonRight = new FlatButton();
|
{
|
buttonRight.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/move-right.png"));
|
buttonRight.setPreferredSize(new Dimension(buttonRight.getStringWidth(), 0));
|
buttonRight.setHorizontalAlignment(SwingConstants.CENTER);
|
buttonRight.setHorizontalTextPosition(SwingConstants.CENTER);
|
buttonRight.getLook().focus = false;
|
buttonRight.getLook().borderRollover = false;
|
buttonRight.getLook().borderSelected = false;
|
buttonRight.getLook().colorBackgroundSelected = null;
|
buttonRight.getLook().colorBackgroundRollover = null;
|
buttonRight.revalidate();
|
}
|
|
buttonCenter = new FlatButton();
|
{
|
buttonCenter.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/move-center.png"));
|
buttonCenter.setPreferredSize(new Dimension(buttonCenter.getStringWidth(), 0));
|
buttonCenter.setHorizontalAlignment(SwingConstants.CENTER);
|
buttonCenter.setHorizontalTextPosition(SwingConstants.CENTER);
|
buttonCenter.getLook().focus = false;
|
buttonCenter.getLook().borderRollover = false;
|
buttonCenter.getLook().borderSelected = false;
|
buttonCenter.getLook().colorBackgroundSelected = null;
|
buttonCenter.getLook().colorBackgroundRollover = null;
|
buttonCenter.revalidate();
|
}
|
|
buttonMinus = new FlatButton();
|
{
|
buttonMinus.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/minus-button.png"));
|
buttonMinus.setPreferredSize(new Dimension(buttonCenter.getStringWidth(), 0));
|
buttonMinus.setHorizontalAlignment(SwingConstants.CENTER);
|
buttonMinus.setHorizontalTextPosition(SwingConstants.CENTER);
|
buttonMinus.getLook().focus = false;
|
buttonMinus.getLook().borderRollover = false;
|
buttonMinus.getLook().borderSelected = false;
|
buttonMinus.getLook().colorBackgroundSelected = null;
|
buttonMinus.getLook().colorBackgroundRollover = null;
|
buttonMinus.revalidate();
|
}
|
|
buttonPlus = new FlatButton();
|
{
|
buttonPlus.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/plus-button.png"));
|
buttonPlus.setPreferredSize(new Dimension(buttonCenter.getStringWidth(), 0));
|
buttonPlus.setHorizontalAlignment(SwingConstants.CENTER);
|
buttonPlus.setHorizontalTextPosition(SwingConstants.CENTER);
|
buttonPlus.getLook().focus = false;
|
buttonPlus.getLook().borderRollover = false;
|
buttonPlus.getLook().borderSelected = false;
|
buttonPlus.getLook().colorBackgroundSelected = null;
|
buttonPlus.getLook().colorBackgroundRollover = null;
|
buttonPlus.revalidate();
|
}
|
|
this.setLayout(null);
|
this.add(buttonMenu);
|
this.add(buttonTop);
|
this.add(buttonLeft);
|
this.add(buttonBottom);
|
this.add(buttonRight);
|
this.add(buttonCenter);
|
this.add(buttonMinus);
|
this.add(buttonPlus);
|
}
|
|
// <editor-fold defaultstate="collapsed" desc="Events">
|
private void events()
|
{
|
this.addComponentListener(new ComponentAdapter()
|
{
|
public void componentResized(ComponentEvent e)
|
{
|
fit();
|
}
|
|
public void componentShown(ComponentEvent e)
|
{
|
fit();
|
}
|
});
|
|
buttonTop.addMouseListener(new MouseAdapter()
|
{
|
public void mousePressed(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
|
translatey = translatey + (30 / scaley);
|
PanelMap.this.repaint();
|
|
timer = new Timer(100, new ActionListener()
|
{
|
public void actionPerformed(ActionEvent evt)
|
{
|
translatey = translatey + (30 / scaley);
|
PanelMap.this.repaint();
|
}
|
});
|
timer.start();
|
}
|
|
public void mouseReleased(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
timer = null;
|
}
|
});
|
|
buttonBottom.addMouseListener(new MouseAdapter()
|
{
|
public void mousePressed(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
translatey = translatey - (30 / scaley);
|
PanelMap.this.repaint();
|
|
timer = new Timer(100, new ActionListener()
|
{
|
public void actionPerformed(ActionEvent evt)
|
{
|
translatey = translatey - (30 / scaley);
|
PanelMap.this.repaint();
|
}
|
});
|
timer.start();
|
}
|
|
public void mouseReleased(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
timer = null;
|
}
|
});
|
|
buttonLeft.addMouseListener(new MouseAdapter()
|
{
|
public void mousePressed(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
translatex = translatex + (30 / scalex);
|
PanelMap.this.repaint();
|
|
timer = new Timer(100, new ActionListener()
|
{
|
public void actionPerformed(ActionEvent evt)
|
{
|
translatex = translatex + (30 / scalex);
|
PanelMap.this.repaint();
|
}
|
});
|
timer.start();
|
}
|
|
public void mouseReleased(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
timer = null;
|
}
|
});
|
|
buttonRight.addMouseListener(new MouseAdapter()
|
{
|
public void mousePressed(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
translatex = translatex - (30 / scalex);
|
PanelMap.this.repaint();
|
|
timer = new Timer(100, new ActionListener()
|
{
|
public void actionPerformed(ActionEvent evt)
|
{
|
translatex = translatex - (30 / scalex);
|
PanelMap.this.repaint();
|
}
|
});
|
timer.start();
|
}
|
|
public void mouseReleased(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
timer = null;
|
}
|
});
|
|
buttonCenter.addActionListener(new ActionListener()
|
{
|
public void actionPerformed(ActionEvent e)
|
{
|
fit();
|
}
|
});
|
|
buttonMinus.addMouseListener(new MouseAdapter()
|
{
|
public void mousePressed(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
zoom(1 / 1.1, getWidth() / 2, getHeight() / 2);
|
|
timer = new Timer(100, new ActionListener()
|
{
|
public void actionPerformed(ActionEvent evt)
|
{
|
zoom(1 / 1.1, getWidth() / 2, getHeight() / 2);
|
}
|
});
|
timer.start();
|
}
|
|
public void mouseReleased(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
timer = null;
|
}
|
});
|
|
buttonPlus.addMouseListener(new MouseAdapter()
|
{
|
public void mousePressed(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
zoom(1.1, getWidth() / 2, getHeight() / 2);
|
|
timer = new Timer(100, new ActionListener()
|
{
|
public void actionPerformed(ActionEvent evt)
|
{
|
zoom(1.1, getWidth() / 2, getHeight() / 2);
|
}
|
});
|
timer.start();
|
}
|
|
public void mouseReleased(MouseEvent e)
|
{
|
if (timer != null)
|
{
|
timer.stop();
|
}
|
timer = null;
|
}
|
});
|
|
this.addMouseListener(new MouseAdapter()
|
{
|
public void mousePressed(MouseEvent e)
|
{
|
setCursor(FlatGUI.lookDesign.getCursor("cursor.pressed"));
|
viewportx = e.getX();
|
viewporty = e.getY();
|
worldx = (viewportx / scalex) - translatex;
|
worldy = (viewporty / scaley) - translatey;
|
selectedObject = getSelected(worldx, worldy);
|
|
if (SwingUtilities.isRightMouseButton(e) == true)
|
{
|
showMenu(e.getX(), e.getY(), false);
|
}
|
else
|
{
|
Rectangle rectangle = new Rectangle(4, getHeight() - buttonMenu.getHeight() - 8,
|
buttonMenu.getWidth(), buttonMenu.getHeight());
|
|
if (rectangle.contains(e.getX(), e.getY()))
|
{
|
showMenu(e.getX(), e.getY(), true);
|
repaint();
|
return;
|
}
|
}
|
|
repaint();
|
}
|
|
public void mouseReleased(MouseEvent e)
|
{
|
dragging = false;
|
setCursor(FlatGUI.lookDesign.getCursor("cursor.hand"));
|
repaint();
|
}
|
});
|
|
this.addMouseMotionListener(new MouseMotionAdapter()
|
{
|
public void mouseMoved(MouseEvent e)
|
{
|
}
|
|
public void mouseDragged(MouseEvent e)
|
{
|
try
|
{
|
dragging = true;
|
worlddraggingx = (e.getX() / scalex) - translatex;
|
worlddraggingy = (e.getY() / scaley) - translatey;
|
|
if ((selectedObject == null) && (lockNavigation == false))
|
{
|
double tx = (e.getX() - viewportx) / scalex;
|
double ty = (e.getY() - viewporty) / scaley;
|
viewportx = e.getX();
|
viewporty = e.getY();
|
translatex = translatex + tx;
|
translatey = translatey + ty;
|
}
|
}
|
catch (Exception exception)
|
{
|
}
|
|
repaint();
|
}
|
});
|
|
this.addMouseWheelListener(new MouseWheelListener()
|
{
|
public void mouseWheelMoved(MouseWheelEvent e)
|
{
|
float increment = (1 + ((float) -e.getWheelRotation() / 10));
|
Point mousePoint = MouseInfo.getPointerInfo().getLocation();
|
Point panelPoint = getLocationOnScreen();
|
double x = mousePoint.getX() - panelPoint.getX();
|
double y = mousePoint.getY() - panelPoint.getY();
|
zoom(increment, x, y);
|
}
|
});
|
|
}
|
|
// </editor-fold>
|
// <editor-fold defaultstate="collapsed" desc="Paint">
|
public void paintMenu(Graphics2D g2)
|
{
|
int x = 4;
|
int y = getHeight() - buttonMenu.getHeight() - 4;
|
buttonMenu.setBounds(x, y, 32, 32);
|
paintComponent(buttonMenu, g2);
|
}
|
|
public void paintPanTiltZoom(Graphics2D g2)
|
{
|
if (lockNavigation == true)
|
{
|
return;
|
}
|
|
int x = getWidth() - 16 - 10 - 48;
|
int y = 12 + 10 + 24;
|
buttonCenter.setBounds(x, y, 32, 32);
|
paintComponent(buttonCenter, g2);
|
|
x = getWidth() - 16 - 10 - 48;
|
y = 12;
|
buttonTop.setBounds(x, y, 32, 32);
|
paintComponent(buttonTop, g2);
|
|
x = getWidth() - 16 - 20 - 72;
|
y = 12 + 10 + 24;
|
buttonLeft.setBounds(x, y, 32, 32);
|
paintComponent(buttonLeft, g2);
|
|
x = getWidth() - 16 - 10 - 48;
|
y = 12 + 20 + 48;
|
buttonBottom.setBounds(x, y, 32, 32);
|
paintComponent(buttonBottom, g2);
|
|
x = getWidth() - 16 - 24;
|
y = 12 + 10 + 24;
|
buttonRight.setBounds(x, y, 32, 32);
|
paintComponent(buttonRight, g2);
|
|
x = getWidth() - 16 - 24;
|
y = 12 + 20 + 64;
|
buttonMinus.setBounds(x, y, 32, 32);
|
paintComponent(buttonMinus, g2);
|
|
x = getWidth() - 16 - 20 - 72;
|
y = 12 + 20 + 64;
|
buttonPlus.setBounds(x, y, 32, 32);
|
paintComponent(buttonPlus, g2);
|
|
}
|
|
public void paintComponent(Component component, Graphics2D g2)
|
{
|
try
|
{
|
Point mousePoint = MouseInfo.getPointerInfo().getLocation();
|
Point panelPoint = getLocationOnScreen();
|
double mousex = mousePoint.getX() - panelPoint.getX();
|
double mousey = mousePoint.getY() - panelPoint.getY();
|
|
Rectangle bounds = component.getBounds();
|
|
g2.translate(bounds.x, bounds.y);
|
|
if (bounds.contains(mousex, mousey) == true)
|
{
|
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
|
}
|
else
|
{
|
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
|
}
|
|
component.paint(g2);
|
g2.translate(-bounds.x, -bounds.y);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
private void showMenu(int x, int y, boolean autopos)
|
{
|
FlatPopupMenu popupMenu = new FlatPopupMenu();
|
|
FlatMenu menuNavigation = new FlatMenu();
|
{
|
menuNavigation.setText("Navigation");
|
menuNavigation.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/earth.png"));
|
{
|
FlatMenuItemCheck item = new FlatMenuItemCheck();
|
item.setText("Lock navigation");
|
item.setStayOpen(true);
|
item.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/globe.png"));
|
item.setSelected(lockNavigation == true);
|
item.addActionListener(new ActionListener()
|
{
|
public void actionPerformed(ActionEvent e)
|
{
|
lockNavigation = ((FlatMenuItemCheck) e.getSource()).isSelected();
|
}
|
});
|
menuNavigation.add(item);
|
}
|
{
|
FlatMenuItemCheck item = new FlatMenuItemCheck();
|
item.setText("Fit to page");
|
item.setStayOpen(true);
|
item.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/adjust-page.png"));
|
item.setSelected(fitToPage == true);
|
item.addActionListener(new ActionListener()
|
{
|
public void actionPerformed(ActionEvent e)
|
{
|
FlatMenuItemCheck item = (FlatMenuItemCheck) e.getSource();
|
fitToPage = item.isSelected();
|
fit();
|
}
|
});
|
menuNavigation.add(item);
|
}
|
}
|
|
FlatMenu menuLayers = new FlatMenu();
|
{
|
try
|
{
|
menuLayers.setText("Layers");
|
menuLayers.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/layers.png"));
|
|
for (String layer : llayer)
|
{
|
double minZoom = ArticScalableVectorGraphics.getAttributeDouble(diagram, layer, "art.minzoom");
|
double maxZoom = ArticScalableVectorGraphics.getAttributeDouble(diagram, layer, "art.maxzoom");
|
boolean showlayer = ((minZoom == 0) && (maxZoom == 0)) || (minZoom > maxZoom)
|
|| ((scalex >= minZoom) && (scalex <= maxZoom));
|
|
if (showlayer == false)
|
{
|
SVGElement element = ArticScalableVectorGraphics.getLayer(diagram, layer);
|
ArticScalableVectorGraphics.setAttribute(element, "display", AnimationElement.AT_CSS, "none",
|
99999);
|
}
|
else
|
{
|
FlatMenuItemCheck item = new FlatMenuItemCheck();
|
{
|
item.setName(layer);
|
item.setText(layer);
|
item.setStayOpen(true);
|
item.setSelected(
|
ArticScalableVectorGraphics.getAttributeString(diagram, item.getName(), "display")
|
.equalsIgnoreCase("none") == false);
|
|
item.addActionListener(new ActionListener()
|
{
|
public void actionPerformed(ActionEvent e)
|
{
|
try
|
{
|
SVGElement element = ArticScalableVectorGraphics.getLayer(diagram,
|
((FlatMenuItemCheck) e.getSource()).getName());
|
|
if (item.isSelected() == false)
|
{
|
// SVG Salamander bug. We can to change all objects recursively.
|
element.setAttribute("art.display", AnimationElement.AT_CSS, "none");
|
ArticScalableVectorGraphics.setAttribute(element, "display",
|
AnimationElement.AT_CSS, "none", 99999);
|
}
|
else
|
{
|
// SVG Salamander bug. We can to change all objects recursively.
|
element.setAttribute("art.display", AnimationElement.AT_CSS, "inline");
|
ArticScalableVectorGraphics.setAttribute(element, "display",
|
AnimationElement.AT_CSS, "inline", 99999);
|
}
|
|
repaint();
|
}
|
catch (Exception exception)
|
{
|
}
|
}
|
});
|
|
if (showlayer == true)
|
{
|
menuLayers.add(item);
|
}
|
}
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
|
}
|
}
|
|
FlatMenu menuDevices = new FlatMenu();
|
{
|
menuDevices.setText("Devices");
|
menuDevices.setIcon(Resources.getResourceURL("data/art.client/icons/24x24/geometrical-shapes-group.png"));
|
}
|
|
popupMenu.add(menuNavigation);
|
popupMenu.add(menuLayers);
|
popupMenu.add(menuDevices);
|
popupMenu.pack();
|
|
if (autopos == true)
|
{
|
popupMenu.show(this, buttonMenu.getX(),
|
buttonMenu.getY() - (int) popupMenu.getPreferredSize().getHeight() - popupMenu.getHeight());
|
}
|
else
|
{
|
popupMenu.show(this, x, y);
|
}
|
}
|
|
// </editor-fold>
|
private Point2D windowToScreen(double x, double y)
|
{
|
double wx = (x / scalex) - translatex;
|
double wy = (y / scaley) - translatey;
|
Point2D resultado = new Point2D.Double(wx, wy);
|
return resultado;
|
}
|
|
private Object getSelected(double x, double y)
|
{
|
return null;
|
}
|
|
private void fit()
|
{
|
if (lockNavigation == true)
|
{
|
return;
|
}
|
|
try
|
{
|
if (fitToPage == true)
|
{
|
Rectangle2D view = diagram.getViewRect();
|
double ratiox = getWidth() / view.getWidth();
|
double ratioy = getHeight() / view.getHeight();
|
double ratio = Math.min(ratiox, ratioy);
|
translatex = -view.getX() + (getWidth() - (view.getWidth() * ratio)) / 2;
|
translatey = -view.getY() + (getHeight() - (view.getHeight() * ratio)) / 2;
|
translatex = translatex / ratio;
|
translatey = translatey / ratio;
|
scalex = ratio;
|
scaley = ratio;
|
}
|
else // Fill to page
|
{
|
Rectangle2D view = diagram.getViewRect();
|
translatex = -view.getX();
|
translatey = -view.getY();
|
scalex = getWidth() / view.getWidth();
|
scaley = getHeight() / view.getHeight();
|
}
|
|
repaint();
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
private void zoom(double increment, double x, double y)
|
{
|
try
|
{
|
if (lockNavigation == true)
|
{
|
return;
|
}
|
|
if ((scalex * increment) < 0.10)
|
{
|
return;
|
}
|
if ((scaley * increment) < 0.10)
|
{
|
return;
|
}
|
|
Point2D O = windowToScreen(0, 0);
|
Point2D P = windowToScreen(x, y);
|
|
// Ponemos el punto del mouse en el (0,0)
|
double tx = P.getX() - O.getX();
|
double ty = P.getY() - O.getY();
|
translatex = translatex - tx;
|
translatey = translatey - ty;
|
|
// Ampliamos
|
scalex = scalex * increment;
|
scaley = scaley * increment;
|
|
// Devolvemos el punto del mouse al punto de pantalla original
|
translatex = translatex + (tx / increment);
|
translatey = translatey + (ty / increment);
|
|
zoomLayer();
|
|
}
|
catch (Exception exception)
|
{
|
}
|
|
repaint();
|
}
|
|
private void zoomLayer()
|
{
|
try
|
{
|
for (String layer : llayer)
|
{
|
SVGElement element = ArticScalableVectorGraphics.getLayer(diagram, layer);
|
double minZoom = ArticScalableVectorGraphics.getAttributeDouble(diagram, layer, "art.minzoom");
|
double maxZoom = ArticScalableVectorGraphics.getAttributeDouble(diagram, layer, "art.maxzoom");
|
boolean showlayer = ((minZoom == 0) && (maxZoom == 0)) || (minZoom > maxZoom)
|
|| ((scalex >= minZoom) && (scalex <= maxZoom));
|
showlayer = showlayer && (ArticScalableVectorGraphics.getAttributeString(diagram, layer, "art.display")
|
.equalsIgnoreCase("none") == false);
|
String display = ArticScalableVectorGraphics.getAttributeString(diagram, layer, "display");
|
|
if ((showlayer == true) && (display.equalsIgnoreCase("none") == true))
|
{
|
ArticScalableVectorGraphics.setAttribute(element, "display", AnimationElement.AT_CSS, "inline",
|
99999);
|
}
|
|
if ((showlayer == false) && (display.equalsIgnoreCase("none") == false))
|
{
|
ArticScalableVectorGraphics.setAttribute(element, "display", AnimationElement.AT_CSS, "none",
|
99999);
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
public static void revalidate(SVGDiagram diagram, Object object)
|
{
|
for (int i = 0; i < diagram.getRoot().getNumChildren(); i++)
|
{
|
revalidate(diagram.getRoot().getChild(i), object);
|
}
|
}
|
|
public static void revalidate(SVGElement element, Object object)
|
{
|
try
|
{
|
|
for (int i = 0; i < element.getNumChildren(); i++)
|
{
|
revalidate(element.getChild(i), object);
|
}
|
|
String sensitive = ArticScalableVectorGraphics.getAttributeString(element, "art.sensitive");
|
|
if (sensitive != null)
|
{
|
String field = ArticScalableVectorGraphics.getAttributeString(element, "art.field");
|
String visibility = ArticScalableVectorGraphics.getAttributeString(element,
|
"art.field." + field + ".visibility");
|
String text = ArticScalableVectorGraphics.getAttributeString(element, "art.field." + field + ".text");
|
String color = ArticScalableVectorGraphics.getAttributeString(element, "art.field." + field + ".color");
|
String style = ArticScalableVectorGraphics.getAttributeString(element, "art.field." + field + ".style");
|
Object objectValue = ArticReflection.getValue(object, field);
|
|
if (visibility != null)
|
{
|
if (evaluate(visibility, objectValue.toString()) == true)
|
{
|
ArticScalableVectorGraphics.setAttribute(element, "display", AnimationElement.AT_AUTO,
|
"display:inline", 99999);
|
}
|
else
|
{
|
ArticScalableVectorGraphics.setAttribute(element, "display", AnimationElement.AT_AUTO, "none",
|
99999);
|
}
|
}
|
|
/**
|
* If text is empty value will be field value There is also the
|
* possibility to introduce a json format
|
*
|
* [ {"Value" : "0", "Text" : "A"}, {"Value" : "1", "Text" :
|
* "B"}, {"Value" : "1", "Text" : "C"} ]
|
*/
|
if (text != null)
|
{
|
Text svgtext = (Text) element;
|
|
if (text.length() == 0)
|
{
|
svgtext.getContent().set(0, objectValue.toString());
|
svgtext.rebuild();
|
}
|
else if (text.contains("@") == true)
|
{
|
svgtext.getContent().set(0, text.replace("@", objectValue.toString()));
|
svgtext.rebuild();
|
}
|
else
|
{
|
for (MyText mytext : (MyText[]) Serialization.deserialize(MyText[].class, text))
|
{
|
if (evaluate(mytext.value, objectValue.toString()) == true)
|
{
|
svgtext.getContent().set(0, mytext.text);
|
svgtext.rebuild();
|
}
|
}
|
}
|
}
|
|
/**
|
* [ {"Value" : "0", "RGBA" : "c000c0ff"} {"Value" : "1", "RGBA"
|
* : "00c000ff"}, {"Value" : "2", "RGBA" : "c00000ff"}, {"Value"
|
* : "3", "RGBA" : "c000c0ff"}, {"Value" : "4", "RGBA" :
|
* "95a5a5ff"}, {"Value" : "5", "RGBA" : "0000c0ff"}, {"Value" :
|
* "6", "RGBA" : "c000c0ff"}, {"Value" : "7", "RGBA" :
|
* "c000c0ff"}, {"Value" : "8", "RGBA" : "c0c000ff"} ]
|
*/
|
if (color != null)
|
{
|
for (MyColor mycolor : (MyColor[]) Serialization.deserialize(MyColor[].class, color))
|
{
|
if (evaluate(mycolor.value, objectValue.toString()) == true)
|
{
|
ArticScalableVectorGraphics.setAttribute(element, "fill", AnimationElement.AT_AUTO,
|
mycolor.getRGB(), 9999);
|
ArticScalableVectorGraphics.setAttribute(element, "fill-opacity", AnimationElement.AT_AUTO,
|
mycolor.getA(), 9999);
|
}
|
}
|
}
|
|
/**
|
*
|
* [ { "Value": 0, "Attributes": [ "fill": "#c000c0","stroke":
|
* "#800080"] }, { "Value": 1, "Attributes": [ "fill":
|
* "#00c000","stroke": "#008000"] }, { "Value": 2, "Attributes":
|
* [ "fill": "#c00000","stroke": "#800000"] }, { "Value": 3,
|
* "Attributes": [ "fill": "#c000c0","stroke": "#800080"] }, {
|
* "Value": 4, "Attributes": [ "fill": "#95a5a5","stroke":
|
* "#6c6c6c"] }, { "Value": 5, "Attributes": [ "fill":
|
* "#0000c0","stroke": "#000080"] }, { "Value": 6, "Attributes":
|
* [ "fill": "#c000c0","stroke": "#800080"] }, { "Value": 7,
|
* "Attributes": [ "fill": "#c000c0","stroke": "#800080"] }, {
|
* "Value": 8, "Attributes": [ "fill": "#ff8000","stroke":
|
* "#804000"] } ]
|
*/
|
if (style != null)
|
{
|
for (MyAttributes myAttributes : (MyAttributes[]) Serialization.deserialize(MyAttributes[].class,
|
style))
|
{
|
if (myAttributes.value.equalsIgnoreCase(objectValue.toString()) == true)
|
{
|
for (MyAttribute myAttribute : myAttributes.attributes)
|
{
|
ArticScalableVectorGraphics.setAttribute(element, myAttribute.name,
|
AnimationElement.AT_AUTO, myAttribute.value, 9999);
|
}
|
}
|
}
|
}
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
private static class MyText
|
{
|
|
@JsonProperty("Value")
|
public String value = null;
|
|
@JsonProperty("Text")
|
public String text = null;
|
}
|
|
private static class MyColor
|
{
|
|
@JsonProperty("Value")
|
public String value = null;
|
|
@JsonProperty("RGBA")
|
public String rgba = null;
|
|
@JsonProperty("RGB")
|
public String rgb = null;
|
|
@JsonProperty("A")
|
public String a = null;
|
|
public String getRGB()
|
{
|
if (rgb == null)
|
{
|
rgb = rgba.substring(0, 7);
|
}
|
return rgb;
|
}
|
|
public String getA()
|
{
|
if (a == null)
|
{
|
a = "" + ((float) Integer.parseInt(rgba.substring(7, 9), 16) / 255.0f);
|
}
|
return a;
|
}
|
}
|
|
private static class MyAttributes
|
{
|
|
@JsonProperty("Value")
|
public String value = null;
|
|
@JsonProperty("Attributes")
|
public MyAttribute[] attributes = new MyAttribute[0];
|
}
|
|
private static class MyAttribute
|
{
|
|
@JsonProperty("Name")
|
public String name = null;
|
|
@JsonProperty("Value")
|
public String value = null;
|
|
}
|
|
private static boolean evaluate(String value1, String value2)
|
{
|
if (value1.contains("!=") == true)
|
{
|
Double number1 = Double.parseDouble(value1.replace("!=", "").trim());
|
Double number2 = Double.parseDouble(value2);
|
return (number1 != number2);
|
}
|
else if (value1.contains("<>") == true)
|
{
|
Double number1 = Double.parseDouble(value1.replace("<>", "").trim());
|
Double number2 = Double.parseDouble(value2);
|
return (number1 != number2);
|
}
|
else if (value1.contains(">=") == true)
|
{
|
Double number1 = Double.parseDouble(value1.replace(">=", "").trim());
|
Double number2 = Double.parseDouble(value2);
|
return (number2 >= number1);
|
}
|
else if (value1.contains("<=") == true)
|
{
|
Double number1 = Double.parseDouble(value1.replace(">=", "").trim());
|
Double number2 = Double.parseDouble(value2);
|
return (number2 <= number1);
|
}
|
else if (value1.contains(">") == true)
|
{
|
Double number1 = Double.parseDouble(value1.replace(">", "").trim());
|
Double number2 = Double.parseDouble(value2);
|
return (number2 > number1);
|
}
|
else if (value1.contains("<") == true)
|
{
|
Double number1 = Double.parseDouble(value1.replace("<", "").trim());
|
Double number2 = Double.parseDouble(value2);
|
return (number2 < number1);
|
}
|
else if (value1.contains("=") == true)
|
{
|
Double number1 = Double.parseDouble(value1.replace("=", "").trim());
|
Double number2 = Double.parseDouble(value2);
|
return (number1 == number2);
|
}
|
else
|
{
|
return value1.equalsIgnoreCase(value2);
|
}
|
}
|
|
public static void setAttribute(SVGElement element, String name, int attributeType, String value, int ttl)
|
{
|
if (ttl < 0)
|
{
|
return;
|
}
|
|
try
|
{
|
element.setAttribute(name, attributeType, value);
|
}
|
catch (Exception e)
|
{
|
}
|
|
for (int i = 0; i < element.getNumChildren(); i++)
|
{
|
setAttribute(element.getChild(i), name, attributeType, value, ttl - 1);
|
}
|
}
|
|
}
|