]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.trend/src/org/simantics/trend/impl/SelectionNode.java
Fixed HistoryUtil.importHistoryArchive to open file in read-only mode
[simantics/platform.git] / bundles / org.simantics.trend / src / org / simantics / trend / impl / SelectionNode.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.trend.impl;
13
14 import java.awt.AlphaComposite;
15 import java.awt.BasicStroke;
16 import java.awt.Color;
17 import java.awt.Composite;
18 import java.awt.Graphics2D;
19 import java.awt.geom.Point2D;
20 import java.awt.geom.Rectangle2D;
21
22 import org.simantics.scenegraph.g2d.G2DNode;
23
24 public class SelectionNode extends G2DNode {
25         
26     private static final long serialVersionUID = -8803833805847402592L;
27
28     public static AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);
29         public static final Color COLOR = new Color(.5f, .5f, 1.f);
30     public static final BasicStroke STROKE =
31             new BasicStroke(1.f,
32                     BasicStroke.CAP_SQUARE,
33                     BasicStroke.CAP_SQUARE,
34                     10.0f, null, 0.0f);
35     
36     double x1, x2, y1, y2;
37     Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, 1, 1);
38     Rectangle2D.Double rect2 = new Rectangle2D.Double(0, 0, 1, 1);
39     boolean binaryZoom = false, timeZoom = false;
40     
41     public void start(Point2D pt, boolean binaryZoom, boolean timeZoom)
42     {
43         x1 = x2 = pt.getX();
44         y1 = y2 = pt.getY();
45         rect.setFrame(x1, y1, 0, 0);    
46         rect2.setFrame(x1, y1, 0, 0);
47         this.binaryZoom = binaryZoom;
48         this.timeZoom = timeZoom;
49     }
50     
51     public void setEndPoint(Point2D pt)
52     {
53         x2 = pt.getX();
54         y2 = pt.getY();
55         layout();
56     }
57     
58     public void layout() {
59         TrendNode trend = (TrendNode) getParent();
60         Plot plot = trend.plot;
61         
62         if (binaryZoom) {
63                 // Binary Zoom
64                 double px = plot.getX();
65                 double py = plot.getY()+plot.analogAreaHeight;
66                 double pw = plot.getWidth();
67                 double ph = plot.binaryAreaHeight;
68                 
69                 rect2.setFrame( Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2) );         
70                 
71                 double _x1 = Math.min( Math.max( x1, px ), px+pw);
72                 double _x2 = Math.min( Math.max( x2, px ), px+pw);
73                                 
74                 rect.setFrame( Math.min(_x1, _x2), py, Math.abs(_x1-_x2), ph );         
75         } else {
76                 // Analog Zoom
77                 double px = plot.getX();
78                 double py = plot.getY();
79                 double pw = plot.getWidth();
80                 double ph = plot.analogAreaHeight;
81                 
82                 rect2.setFrame( Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2) );         
83                 
84                 double _x1 = Math.min( Math.max( x1, px ), px+pw);
85                 double _x2 = Math.min( Math.max( x2, px ), px+pw);
86                 double _y1 = Math.min( Math.max( y1, py ), py+ph);
87                 double _y2 = Math.min( Math.max( y2, py ), py+ph);
88                                 
89                 if (timeZoom) {
90                 rect.setFrame( Math.min(_x1, _x2), py, Math.abs(_x1-_x2), plot.analogAreaHeight );                              
91                 } else {
92                 rect.setFrame( Math.min(_x1, _x2), Math.min(_y1, _y2), Math.abs(_x1-_x2), Math.abs(_y1-_y2) );                                  
93                 }
94         }       
95         
96     }
97
98         @Override
99         public void render(Graphics2D g2d) {            
100                 Composite c = g2d.getComposite();
101                 g2d.setComposite( composite );
102                 g2d.setColor( COLOR );
103                 g2d.fill( rect );
104                 g2d.setComposite( c );
105                 g2d.setStroke( STROKE );
106                 g2d.draw( rect );
107         }
108         
109         @Override
110         public Rectangle2D getBoundsInLocal() {         
111                 return rect;
112         }
113
114 }