5 import java.awt.event.*;
7 import java.awt.datatransfer.*;
9 import ij.plugin.filter.Analyzer;
19 public class TextPanel extends JPanel implements AdjustmentListener,
20 MouseListener, MouseMotionListener, KeyListener, ClipboardOwner,
24 int iGridWidth,iGridHeight;
30 int iColCount,iRowCount;
31 int iRowHeight,iFirstRow;
33 Scrollbar sbHoriz,sbVert;
34 int iSbWidth,iSbHeight;
38 boolean headings =
true;
41 KeyListener keyListener;
42 Cursor resizeCursor =
new Cursor(Cursor.E_RESIZE_CURSOR);
43 Cursor defaultCursor =
new Cursor(Cursor.DEFAULT_CURSOR);
44 int selStart=-1, selEnd=-1,selOrigin=-1, selLine=-1;
47 boolean columnsManuallyAdjusted;
51 tc =
new TextCanvas(
this);
52 setLayout(
new BorderLayout());
54 sbHoriz=
new Scrollbar(Scrollbar.HORIZONTAL);
55 sbHoriz.addAdjustmentListener(
this);
56 add(
"South", sbHoriz);
57 sbVert=
new Scrollbar(Scrollbar.VERTICAL);
58 sbVert.addAdjustmentListener(
this);
61 sbHoriz.addKeyListener(ij);
62 sbVert.addKeyListener(ij);
71 if (title.equals(
"Results")) {
73 addPopupItem(
"Clear Results");
74 addPopupItem(
"Summarize");
75 addPopupItem(
"Set Measurements...");
81 addPopupItem(
"Save As...");
85 addPopupItem(
"Clear");
86 addPopupItem(
"Select All");
87 addPopupItem(
"Copy All");
91 void addPopupItem(String s) {
92 JMenuItem mi=
new JMenuItem(s);
93 mi.addActionListener(
this);
103 boolean sameLabels = labels.equals(this.labels);
104 this.labels = labels;
105 if (labels.equals(
"")) {
107 sColHead=
new String[1];
110 StringTokenizer t =
new StringTokenizer(labels,
"\t");
111 iColCount = t.countTokens();
112 sColHead=
new String[iColCount];
113 for(
int i=0; i<iColCount; i++)
114 sColHead[i] = t.nextToken();
118 if (!(iColWidth!=null && iColWidth.length==iColCount && sameLabels && iColCount!=1)) {
119 iColWidth=
new int[iColCount];
120 columnsManuallyAdjusted =
false;
130 return labels==null?
"":labels;
133 public void setFont(Font font) {
141 char[] chars = data.toCharArray();
142 vData.addElement(chars);
145 if (iColCount==1 && tc.fMetrics!=null) {
146 iColWidth[0] = Math.max(iColWidth[0], tc.fMetrics.charsWidth(chars,0,chars.length));
155 if (data==null) data=
"null";
159 int p=data.indexOf(
'\n');
161 appendWithoutUpdate(data);
164 appendWithoutUpdate(data.substring(0,p));
165 data = data.substring(p+1);
174 void appendWithoutUpdate(String data) {
175 char[] chars = data.toCharArray();
176 vData.addElement(chars);
180 void updateDisplay() {
181 iY=iRowHeight*(iRowCount+1);
183 if (iColCount>1 && iRowCount<=10 && !columnsManuallyAdjusted)
188 String getCell(
int column,
int row) {
189 if (column<0||column>=iColCount||row<0||row>=iRowCount)
191 return new String(tc.getChars(column, row));
194 synchronized void adjustVScroll() {
195 if(iRowHeight==0)
return;
196 Dimension d = tc.getSize();
197 int value = iY/iRowHeight;
198 int visible = d.height/iRowHeight;
199 int maximum = iRowCount+1;
200 if (visible<0) visible=0;
201 if (visible>maximum) visible=maximum;
202 if (value>(maximum-visible)) value=maximum-visible;
203 sbVert.setValues(value,visible,0,maximum);
207 synchronized void adjustHScroll() {
208 if(iRowHeight==0)
return;
209 Dimension d = tc.getSize();
211 for(
int i=0;i<iColCount;i++)
214 sbHoriz.setValues(iX,d.width,0,iGridWidth);
215 iX=sbHoriz.getValue();
218 public void adjustmentValueChanged (AdjustmentEvent e) {
219 iX=sbHoriz.getValue();
220 iY=iRowHeight*sbVert.getValue();
224 public void mousePressed (MouseEvent e) {
225 int x=e.getX(), y=e.getY();
226 if (e.isPopupTrigger() || e.isMetaDown())
227 pm.show(e.getComponent(),x,y);
228 else if (e.isShiftDown())
229 extendSelection(x, y);
234 public void mouseExited (MouseEvent e) {
236 setCursor(defaultCursor);
241 public void mouseMoved (MouseEvent e) {
242 int x=e.getX(), y=e.getY();
248 if(x>-7 && x<7)
break;
253 setCursor(resizeCursor);
255 iXDrag=xb-iColWidth[i];
262 setCursor(defaultCursor);
267 public void mouseDragged (MouseEvent e) {
268 if (e.isPopupTrigger() || e.isMetaDown())
270 int x=e.getX(), y=e.getY();
271 if(bDrag && x<tc.getSize().width) {
274 iColWidth[iColDrag]=w;
275 columnsManuallyAdjusted =
true;
279 extendSelection(x, y);
283 public void mouseReleased (MouseEvent e) {}
284 public void mouseClicked (MouseEvent e) {}
285 public void mouseEntered (MouseEvent e) {}
289 keyListener = listener;
292 public void keyPressed (KeyEvent e) {
293 boolean cutCopyOK = (e.isControlDown()||e.isMetaDown())
294 && selStart!=-1 && selEnd!=-1;
295 if (cutCopyOK && e.getKeyCode()==KeyEvent.VK_C)
297 else if (cutCopyOK && e.getKeyCode()==KeyEvent.VK_X)
299 else if (keyListener!=null)
300 keyListener.keyPressed(e);
303 public void keyReleased (KeyEvent e) {}
304 public void keyTyped (KeyEvent e) {}
306 public void actionPerformed (ActionEvent e) {
307 String cmd=e.getActionCommand();
311 void doCommand(String cmd) {
314 if (cmd.equals(
"Save As..."))
316 else if (cmd.equals(
"Cut"))
318 else if (cmd.equals(
"Copy"))
320 else if (cmd.equals(
"Clear"))
322 else if (cmd.equals(
"Select All"))
324 else if (cmd.equals(
"Copy All")) {
328 }
else if (cmd.equals(
"Summarize"))
329 IJ.doCommand(
"Summarize");
330 else if (cmd.equals(
"Clear Results"))
331 IJ.doCommand(
"Clear Results");
332 else if (cmd.equals(
"Set Measurements..."))
333 IJ.doCommand(
"Set Measurements...");
336 public void lostOwnership (Clipboard clip, Transferable cont) {}
338 void select(
int x,
int y) {
339 Dimension d = tc.getSize();
340 if(iRowHeight==0 || x>d.width || y>d.height)
342 int r=(y/iRowHeight)-1+iFirstRow;
343 if(r>=0 && r<iRowCount && x<iGridWidth) {
351 selOrigin = iRowCount-1;
358 void extendSelection(
int x,
int y) {
359 Dimension d = tc.getSize();
360 if(iRowHeight==0 || x>d.width || y>d.height)
362 int r=(y/iRowHeight)-1+iFirstRow;
364 if(r>=0 && r<iRowCount) {
370 selStart = selOrigin;
383 if (selStart==-1 || selEnd==-1)
return 0;
384 StringBuffer sb =
new StringBuffer();
385 for (
int i=selStart; i<=selEnd; i++) {
386 char[] chars = (
char[])(vData.elementAt(i));
390 String s =
new String(sb);
391 Clipboard clip = getToolkit().getSystemClipboard();
392 if (clip==null)
return 0;
393 StringSelection cont =
new StringSelection(s);
394 clip.setContents(cont,
this);
396 IJ.
showStatus((selEnd-selStart+1)+
" lines copied to clipboard");
397 if (this.getParent() instanceof
ImageJ)
405 if (selStart==-1 || selEnd==-1)
407 if (selStart==0 && selEnd==(iRowCount-1)) {
408 vData.removeAllElements();
415 int count = selEnd-selStart+1;
416 for (
int i=0; i<count; i++) {
417 vData.removeElementAt(selStart);
421 selStart=-1; selEnd=-1; selOrigin=-1; selLine=-1;
429 selEnd = iRowCount-1;
446 public void save(PrintWriter pw) {
448 if (labels!=null && !labels.equals(
""))
450 for (
int i=0; i<iRowCount; i++) {
451 char[] chars = (
char[])(vData.elementAt(i));
452 pw.println(
new String(chars));
461 public void setTitle(String title) {
474 if (index<0 || index>=iRowCount)
475 throw new IllegalArgumentException(
"index out of range: "+index);
476 return new String((
char[])(vData.elementAt(index)));
481 vData.removeAllElements();