KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > forms > widgets > SelectionData


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.forms.widgets;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.MouseEvent;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.widgets.Display;
19
20 public class SelectionData {
21     public Display display;
22     public Color bg;
23     public Color fg;
24     private Point start;
25     private Point stop;
26     private ArrayList JavaDoc segments;
27     private boolean newLineNeeded;
28     
29     public SelectionData(MouseEvent e) {
30         display = e.display;
31         segments = new ArrayList JavaDoc();
32         start = new Point(e.x, e.y);
33         stop = new Point(e.x, e.y);
34         bg = e.display.getSystemColor(SWT.COLOR_LIST_SELECTION);
35         fg = e.display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
36     }
37     
38     public void markNewLine() {
39         newLineNeeded=true;
40     }
41     public void addSegment(String JavaDoc text) {
42         if (newLineNeeded) {
43             segments.add(System.getProperty("line.separator")); //$NON-NLS-1$
44
newLineNeeded=false;
45         }
46         segments.add(text);
47     }
48
49     public void update(MouseEvent e) {
50         //Control c = (Control)e.widget;
51
stop.x = e.x;
52         stop.y = e.y;
53     }
54     public void reset() {
55         segments.clear();
56     }
57     public String JavaDoc getSelectionText() {
58         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
59         for (int i=0; i<segments.size(); i++) {
60             buf.append((String JavaDoc)segments.get(i));
61         }
62         return buf.toString();
63     }
64     public boolean canCopy() {
65         return segments.size()>0;
66     }
67     
68     private int getTopOffset() {
69         return start.y<stop.y?start.y:stop.y;
70     }
71     private int getBottomOffset() {
72         return start.y>stop.y?start.y:stop.y;
73     }
74     public int getLeftOffset(Locator locator) {
75         return isInverted(locator)? stop.x:start.x;
76     }
77     public int getLeftOffset(int rowHeight) {
78         return isInverted(rowHeight) ? stop.x:start.x;
79     }
80     public int getRightOffset(Locator locator) {
81         return isInverted(locator)? start.x: stop.x;
82     }
83     public int getRightOffset(int rowHeight) {
84         return isInverted(rowHeight) ? start.x:stop.x;
85     }
86     private boolean isInverted(Locator locator) {
87         int rowHeight = ((int [])locator.heights.get(locator.rowCounter))[0];
88         return isInverted(rowHeight);
89     }
90     private boolean isInverted(int rowHeight) {
91         int deltaY = start.y - stop.y;
92         if (Math.abs(deltaY) > rowHeight) {
93             // inter-row selection
94
return deltaY>0;
95         }
96         // intra-row selection
97
return start.x > stop.x;
98     }
99     public boolean isEnclosed() {
100         return !start.equals(stop);
101     }
102
103     public boolean isSelectedRow(Locator locator) {
104         if (!isEnclosed())
105             return false;
106         int rowHeight = ((int [])locator.heights.get(locator.rowCounter))[0];
107         return isSelectedRow(locator.y, rowHeight);
108     }
109     public boolean isSelectedRow(int y, int rowHeight) {
110         if (!isEnclosed())
111             return false;
112         return (y + rowHeight >= getTopOffset() &&
113                 y <= getBottomOffset());
114     }
115     public boolean isFirstSelectionRow(Locator locator) {
116         if (!isEnclosed())
117             return false;
118         int rowHeight = ((int [])locator.heights.get(locator.rowCounter))[0];
119         return (locator.y + rowHeight >= getTopOffset() &&
120                 locator.y <= getTopOffset());
121     }
122     public boolean isFirstSelectionRow(int y, int rowHeight) {
123         if (!isEnclosed())
124             return false;
125         return (y + rowHeight >= getTopOffset() &&
126                 y <= getTopOffset());
127     }
128     public boolean isLastSelectionRow(Locator locator) {
129         if (!isEnclosed())
130             return false;
131         int rowHeight = ((int [])locator.heights.get(locator.rowCounter))[0];
132         return (locator.y + rowHeight >=getBottomOffset() &&
133                 locator.y <= getBottomOffset());
134     }
135     public boolean isLastSelectionRow(int y, int rowHeight) {
136         if (!isEnclosed())
137             return false;
138         return (y + rowHeight >=getBottomOffset() &&
139                 y <= getBottomOffset());
140     }
141 }
142
Popular Tags