~ubuntu-branches/debian/sid/geogebra/sid

« back to all changes in this revision

Viewing changes to geogebra/gui/view/spreadsheet/InspectorView.java

  • Committer: Package Import Robot
  • Author(s): Giovanni Mascellani
  • Date: 2012-01-10 11:37:41 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120110113741-satwohsd4de4ite1
Tags: 4.0.19.0+dfsg1-1
* New upstream version (closes: #649893).
* Update dependency: icedtea-plugin -> icedtea-netx-common (LP: #893007).
* New thumbnailer configuration compatible with Gnome 3.
* Package building is now managed by javahelper instead of upstream
  build.xml.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
GeoGebra - Dynamic Mathematics for Everyone
 
3
http://www.geogebra.org
 
4
 
 
5
This file is part of GeoGebra.
 
6
 
 
7
This program is free software; you can redistribute it and/or modify it 
 
8
under the terms of the GNU General Public License as published by 
 
9
the Free Software Foundation.
 
10
 
 
11
 */
 
12
 
 
13
package geogebra.gui.view.spreadsheet;
 
14
 
 
15
import geogebra.gui.InputDialog;
 
16
import geogebra.gui.inputfield.MyTextField;
 
17
import geogebra.gui.util.GeoGebraIcon;
 
18
import geogebra.gui.util.PopupMenuButton;
 
19
import geogebra.gui.util.SelectionTable;
 
20
import geogebra.kernel.Construction;
 
21
import geogebra.kernel.GeoElement;
 
22
import geogebra.kernel.GeoList;
 
23
import geogebra.kernel.GeoPoint;
 
24
import geogebra.kernel.GeoPolygon;
 
25
import geogebra.kernel.GeoText;
 
26
import geogebra.kernel.Kernel;
 
27
import geogebra.kernel.View;
 
28
import geogebra.kernel.arithmetic.NumberValue;
 
29
import geogebra.kernel.kernelND.GeoPointND;
 
30
import geogebra.main.Application;
 
31
 
 
32
import java.awt.BorderLayout;
 
33
import java.awt.Color;
 
34
import java.awt.Component;
 
35
import java.awt.Container;
 
36
import java.awt.Dimension;
 
37
import java.awt.FlowLayout;
 
38
import java.awt.event.ActionEvent;
 
39
import java.awt.event.ActionListener;
 
40
import java.awt.event.KeyEvent;
 
41
import java.awt.event.KeyListener;
 
42
import java.awt.event.MouseEvent;
 
43
import java.awt.event.MouseListener;
 
44
import java.util.ArrayList;
 
45
 
 
46
import javax.swing.BorderFactory;
 
47
import javax.swing.BoxLayout;
 
48
import javax.swing.JButton;
 
49
import javax.swing.JCheckBox;
 
50
import javax.swing.JCheckBoxMenuItem;
 
51
import javax.swing.JComboBox;
 
52
import javax.swing.JLabel;
 
53
import javax.swing.JPanel;
 
54
import javax.swing.JScrollPane;
 
55
import javax.swing.JTable;
 
56
import javax.swing.JTextField;
 
57
import javax.swing.JViewport;
 
58
import javax.swing.event.ListSelectionEvent;
 
59
import javax.swing.event.ListSelectionListener;
 
60
import javax.swing.table.DefaultTableCellRenderer;
 
61
import javax.swing.table.DefaultTableModel;
 
62
import javax.swing.table.TableCellRenderer;
 
63
import javax.swing.table.TableColumn;
 
64
 
 
65
 
 
66
/**
 
67
 * View for inspecting selected GeoElements
 
68
 * 
 
69
 * @author G. Sturr, 2011-2-12
 
70
 * 
 
71
 */
 
72
 
 
73
public class InspectorView extends InputDialog implements View, MouseListener, ListSelectionListener, KeyListener, ActionListener{
 
74
 
 
75
        private static final Color EVEN_ROW_COLOR = new Color(241, 245, 250);
 
76
        private static final Color TABLE_GRID_COLOR = new Color(0xd9d9d9);
 
77
        private static final int minRows = 10;
 
78
 
 
79
        // column types
 
80
        private static final int COL_DERIVATIVE = 0;
 
81
        private static final int COL_DERIVATIVE2 = 1;
 
82
        private static final int COL_DIFFERENCE = 2;
 
83
        private static final int COL_CURVATURE = 3;
 
84
 
 
85
 
 
86
        // ggb fields
 
87
        private Kernel kernel;
 
88
        private Construction cons;
 
89
 
 
90
        // table fields
 
91
        private JTable table;
 
92
        private DefaultTableModel model;
 
93
        private ArrayList<Integer> extraColumnList;
 
94
 
 
95
        // GUI 
 
96
        private JLabel lblGeoName, lblStart, lblStep;
 
97
        private MyTextField fldStart, fldStep;
 
98
        private JPanel controlPanel;
 
99
        private JCheckBox ckShowTangent, ckShowOscCircle, ckShowX, ckShowY, ckShowAutoTable;
 
100
        private JComboBox cbShow, cbAdd;
 
101
        private JButton btnAdd, btnRemove;
 
102
        private String[] columnNames;
 
103
 
 
104
 
 
105
        // Geos
 
106
        private GeoElement selectedGeo, tangentLine, oscCircle, xSegment, ySegment;
 
107
        private GeoElement derivative, derivative2;
 
108
        private GeoPoint testPoint;
 
109
        private GeoList pts;
 
110
        private double start =-1;
 
111
        private double step = 0.1;
 
112
        private JLabel lblShow;
 
113
        private ArrayList<GeoElement> geoList;
 
114
        private PopupMenuButton btnAddColumn;
 
115
        private ArrayList<GeoElement> showGeo;
 
116
        private ArrayList<String> showName;
 
117
        private PopupMenuButton btnShow;
 
118
 
 
119
 
 
120
 
 
121
        /** Constructor */
 
122
        public InspectorView(Application app) {
 
123
 
 
124
                super(app.getFrame(), false);
 
125
                this.app = app; 
 
126
                kernel = app.getKernel();
 
127
                cons = kernel.getConstruction();
 
128
                boolean showApply = false;
 
129
 
 
130
                // setup InputDialog GUI
 
131
                String title = app.getMenu("FunctionInspector");
 
132
                createGUI(title, "", false, 16, 1, false, false, false, false, false, showApply, false);
 
133
                this.btOK.setVisible(false);
 
134
                this.btCancel.setVisible(false);
 
135
 
 
136
 
 
137
                // create Table and additional GUI elements
 
138
                geoList = new ArrayList<GeoElement>();
 
139
                createTable();
 
140
                createGUIElements();
 
141
 
 
142
 
 
143
                // put additional GUI together
 
144
                JPanel cp1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
 
145
                cp1.add(lblStart);
 
146
                cp1.add(fldStart);
 
147
                cp1.add(lblStep);
 
148
                cp1.add(fldStep);
 
149
                cp1.add(btnAddColumn);
 
150
                cp1.add(btnRemove);
 
151
 
 
152
                JPanel cp2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
 
153
                cp2.add(lblShow);
 
154
                cp2.add(ckShowTangent);
 
155
                cp2.add(ckShowX);
 
156
                cp2.add(ckShowY);
 
157
                cp2.add(ckShowOscCircle);
 
158
                //cp2.add(btnShow);
 
159
 
 
160
 
 
161
 
 
162
 
 
163
                controlPanel = new JPanel();
 
164
                controlPanel.setLayout(new BoxLayout(controlPanel,BoxLayout.Y_AXIS));
 
165
                controlPanel.add(cp1);
 
166
                controlPanel.add(cp2);
 
167
                //controlPanel.add(cp3);
 
168
                controlPanel.setVisible(false);
 
169
 
 
170
                JPanel southPanel = new JPanel(new BorderLayout());
 
171
                southPanel.add(controlPanel,BorderLayout.SOUTH);
 
172
                southPanel.setMinimumSize(controlPanel.getPreferredSize());
 
173
 
 
174
//System.out.println();
 
175
 
 
176
                JScrollPane scroller = new JScrollPane(table);
 
177
                scroller.setPreferredSize(table.getPreferredSize());
 
178
 
 
179
                JPanel headerPanel = new JPanel(new BorderLayout());
 
180
                headerPanel.add(lblGeoName, BorderLayout.CENTER);
 
181
                headerPanel.setBorder(BorderFactory.createEmptyBorder(2,5,2,2));
 
182
 
 
183
                JPanel centerPanel = new JPanel(new BorderLayout(5,5));
 
184
                centerPanel.add(headerPanel,BorderLayout.NORTH);
 
185
                centerPanel.add(scroller,BorderLayout.CENTER);
 
186
                centerPanel.add(southPanel,BorderLayout.SOUTH);
 
187
 
 
188
                getContentPane().add(centerPanel,BorderLayout.CENTER);
 
189
 
 
190
 
 
191
                centerOnScreen();
 
192
                setResizable(true);
 
193
                updateFont();
 
194
                app.getKernel().attach(this);
 
195
        }
 
196
 
 
197
 
 
198
 
 
199
        //  Create GUI elements 
 
200
        // =====================================
 
201
 
 
202
        private void createGUIElements(){
 
203
 
 
204
                columnNames = new String[4];
 
205
                columnNames[COL_DERIVATIVE] =   app.getPlain("fncInspector.Derivative");
 
206
                columnNames[COL_DERIVATIVE2] =  app.getPlain("fncInspector.Derivative2");
 
207
                columnNames[COL_CURVATURE] =    app.getPlain("fncInspector.Curvature");
 
208
                columnNames[COL_DIFFERENCE] =   app.getPlain("fncInspector.Difference");
 
209
 
 
210
                lblGeoName = new JLabel(getTitleString());
 
211
                lblGeoName.setFont(app.getBoldFont());
 
212
 
 
213
                lblStep = new JLabel(app.getMenu("Step") + ":");
 
214
                lblStart = new JLabel(app.getMenu("Start") + ":");
 
215
                lblShow = new JLabel(app.getMenu("Show") + ":");
 
216
                fldStep = new MyTextField(app);
 
217
                fldStep.addActionListener(this);
 
218
                fldStep.setColumns(6);
 
219
 
 
220
                fldStart = new MyTextField(app);
 
221
                fldStart.addActionListener(this);
 
222
                fldStart.setColumns(6);
 
223
 
 
224
                ckShowTangent = new JCheckBox(app.getMenu("Tangent"));
 
225
                ckShowOscCircle = new JCheckBox(app.getMenu("OsculatingCircle"));
 
226
                ckShowX = new JCheckBox(app.getMenu("Xseg"));
 
227
                ckShowY = new JCheckBox(app.getMenu("Yseg"));
 
228
                ckShowX.setSelected(true);
 
229
                ckShowY.setSelected(true);
 
230
                
 
231
                
 
232
                ckShowTangent.addActionListener(this);
 
233
                ckShowOscCircle.addActionListener(this);
 
234
                ckShowX.addActionListener(this);
 
235
                ckShowY.addActionListener(this);
 
236
 
 
237
 
 
238
                btnAdd = new JButton("\u271A");
 
239
                btnAdd.addActionListener(this);
 
240
 
 
241
                btnRemove = new JButton("\u2718");
 
242
                btnRemove.addActionListener(this);
 
243
 
 
244
                
 
245
                
 
246
                btnAddColumn = new PopupMenuButton(app, columnNames, -1, 1, 
 
247
                                new Dimension(0, 12), SelectionTable.MODE_TEXT);
 
248
                
 
249
                btnAddColumn.setKeepVisible(false);
 
250
                btnAddColumn.setStandardButton(true);
 
251
                btnAddColumn.setFixedIcon(GeoGebraIcon.createDownTriangleIcon(10));
 
252
                btnAddColumn.setText("Add Column");
 
253
                btnAddColumn.addActionListener(this);
 
254
 
 
255
                buildShowButton();
 
256
 
 
257
        }
 
258
 
 
259
 
 
260
        /** 
 
261
         * Builds popup button with checkbox menu items to hide/show display geos
 
262
         */
 
263
        private void buildShowButton(){
 
264
 
 
265
                btnShow = new PopupMenuButton(app);
 
266
                btnShow.setKeepVisible(true);
 
267
                btnShow.setStandardButton(true);
 
268
                btnShow.setFixedIcon(GeoGebraIcon.createDownTriangleIcon(10));
 
269
                btnShow.setText("Show" + "...");
 
270
 
 
271
                JCheckBoxMenuItem menuItem;
 
272
                menuItem = new JCheckBoxMenuItem(app.getMenu("XLine"));
 
273
                menuItem.addActionListener(new ActionListener(){
 
274
                        public void actionPerformed(ActionEvent e) {
 
275
                                xSegment.setEuclidianVisible(!xSegment.isEuclidianVisible());
 
276
                        }
 
277
                });
 
278
                btnShow.addPopupMenuItem(menuItem);
 
279
                
 
280
                menuItem = new JCheckBoxMenuItem(app.getMenu("YLine"));
 
281
                menuItem.addActionListener(new ActionListener(){
 
282
                        public void actionPerformed(ActionEvent e) {
 
283
                                ySegment.setEuclidianVisible(!ySegment.isEuclidianVisible());
 
284
                        }
 
285
                });
 
286
                btnShow.addPopupMenuItem(menuItem);
 
287
                
 
288
                menuItem = new JCheckBoxMenuItem(app.getMenu("Tangent"));
 
289
                menuItem.addActionListener(new ActionListener(){
 
290
                        public void actionPerformed(ActionEvent e) {
 
291
                                tangentLine.setEuclidianVisible(!tangentLine.isEuclidianVisible());
 
292
                        }
 
293
                });
 
294
                btnShow.addPopupMenuItem(menuItem);
 
295
                
 
296
                menuItem = new JCheckBoxMenuItem(app.getMenu("OsculatingCircle"));
 
297
                menuItem.addActionListener(new ActionListener(){
 
298
                        public void actionPerformed(ActionEvent e) {
 
299
                                tangentLine.setEuclidianVisible(!tangentLine.isEuclidianVisible());
 
300
                        }
 
301
                });
 
302
                btnShow.addPopupMenuItem(menuItem);
 
303
 
 
304
                
 
305
 
 
306
        }
 
307
 
 
308
 
 
309
        //  Create/Setup Table 
 
310
        // =====================================
 
311
 
 
312
        private void createTable(){
 
313
 
 
314
                table = new JTable(minRows,2){
 
315
 
 
316
                        // disable cell editing
 
317
                        @Override
 
318
                        public boolean isCellEditable(int rowIndex, int colIndex) {
 
319
                                return false;   
 
320
                        }
 
321
 
 
322
                        // fill empty scroll pane space with table background color
 
323
                        @Override
 
324
                        protected void configureEnclosingScrollPane() {
 
325
                                super.configureEnclosingScrollPane();
 
326
                                Container p = getParent();
 
327
                                if (p instanceof JViewport) {
 
328
                                        ((JViewport) p).setBackground(getBackground());
 
329
                                }
 
330
                        }
 
331
 
 
332
                        // shade alternate rows
 
333
                        private Color rowColor(int row){
 
334
                                Color c;
 
335
                                if (row % 2 == 0) 
 
336
                                        c = EVEN_ROW_COLOR;
 
337
                                else 
 
338
                                        c = this.getBackground();
 
339
                                return c;
 
340
                        }
 
341
 
 
342
 
 
343
                        public Component prepareRenderer(TableCellRenderer renderer,int row, int column) {
 
344
                                Component c = super.prepareRenderer(renderer, row, column);
 
345
 
 
346
                                if (isCellSelected(row, column)) {
 
347
                                        c.setBackground(getSelectionBackground());
 
348
                                        c.setForeground(getSelectionForeground());
 
349
                                } else {
 
350
                                        c.setBackground(rowColor(row));
 
351
                                        c.setForeground(getForeground());
 
352
                                }
 
353
                                setFont(app.getPlainFont());
 
354
                                return c;
 
355
                        }
 
356
                };
 
357
 
 
358
                int vColIndex = 0;
 
359
                TableColumn col = table.getColumnModel().getColumn(vColIndex);
 
360
                col.setCellRenderer(new MyCellRenderer());
 
361
 
 
362
                table.setShowGrid(true);
 
363
                table.setGridColor(TABLE_GRID_COLOR);
 
364
                //table.setAutoCreateColumnsFromModel(false);
 
365
                table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
 
366
                //table.setPreferredScrollableViewportSize(table.getPreferredSize());
 
367
                table.setBorder(null);
 
368
                table.getSelectionModel().addListSelectionListener(this);
 
369
                table.addKeyListener(this);
 
370
 
 
371
                // create list to store column types of dynamically appended columns 
 
372
                extraColumnList = new ArrayList<Integer>();
 
373
 
 
374
        }
 
375
 
 
376
        public class MyCellRenderer extends DefaultTableCellRenderer  {
 
377
 
 
378
                public Component getTableCellRendererComponent(JTable table, Object value, 
 
379
                                boolean isSelected, boolean hasFocus, final int row, int column) {
 
380
 
 
381
                        setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
 
382
                        setText((String) value);
 
383
                        return this;
 
384
 
 
385
                }
 
386
 
 
387
        }
 
388
 
 
389
 
 
390
 
 
391
        //     Table Update
 
392
        // =====================================
 
393
 
 
394
        private void populateTableModel(){
 
395
 
 
396
                String lbl = selectedGeo.getLabel();
 
397
                ArrayList<String> property = new ArrayList<String>();
 
398
                ArrayList<String> value = new ArrayList<String>();
 
399
 
 
400
 
 
401
                switch (selectedGeo.getGeoClassType()){
 
402
 
 
403
                case GeoElement.GEO_CLASS_POINT:
 
404
 
 
405
                        property.add(app.getCommand("Angle"));
 
406
                        value.add("Angle[" + lbl + "]");
 
407
                        break;
 
408
 
 
409
                case GeoElement.GEO_CLASS_POLYGON:
 
410
 
 
411
                        GeoPointND[] pts =  ((GeoPolygon)selectedGeo).getPoints();
 
412
                        for(int i=0; i<pts.length; i++){
 
413
                                property.add(pts[i].getLabel());
 
414
                                value.add(((GeoElement) pts[i]).toDefinedValueString());
 
415
                        }
 
416
 
 
417
                        property.add(app.getCommand("Area"));
 
418
                        value.add("Area[" + lbl + "]");
 
419
 
 
420
                        property.add(app.getCommand("Perimeter"));
 
421
                        value.add("Perimeter[" + lbl + "]");
 
422
 
 
423
                        break;
 
424
 
 
425
                case GeoElement.GEO_CLASS_LIST:
 
426
 
 
427
                        GeoList g = (GeoList) selectedGeo;
 
428
                        if(((GeoElement)g).getGeoElementForPropertiesDialog().isNumberValue()){
 
429
 
 
430
                                property.add(app.getCommand("Mean"));
 
431
                                value.add("Mean[" + lbl + "]");
 
432
 
 
433
                                property.add(app.getCommand("SD"));
 
434
                                value.add("SD[" + lbl + "]");
 
435
 
 
436
                                property.add(app.getCommand("Min"));
 
437
                                value.add("Min[" + lbl + "]");
 
438
 
 
439
                                property.add(app.getCommand("Q1"));
 
440
                                value.add("Q1[" + lbl + "]");
 
441
 
 
442
                                property.add(app.getCommand("Median"));
 
443
                                value.add("Median[" + lbl + "]");
 
444
 
 
445
                                property.add(app.getCommand("Q3"));
 
446
                                value.add("Q3[" + lbl + "]");
 
447
 
 
448
                                property.add(app.getCommand("Max"));
 
449
                                value.add("Max[" + lbl + "]");
 
450
 
 
451
                        }
 
452
                        break;
 
453
 
 
454
 
 
455
                case GeoElement.GEO_CLASS_CONIC:
 
456
 
 
457
                        property.add(app.getCommand("Center"));
 
458
                        value.add("Center[" + lbl + "]");
 
459
 
 
460
                        property.add(app.getCommand("Radius"));
 
461
                        value.add("Radius[" + lbl + "]");
 
462
 
 
463
                        property.add(app.getCommand("Area"));
 
464
                        value.add("Area[" + lbl + "]");
 
465
 
 
466
                        property.add(app.getCommand("Circumference"));
 
467
                        value.add("Circumference[" + lbl + "]");
 
468
 
 
469
                        property.add(app.getCommand("Eccentricity"));
 
470
                        value.add("Eccentricity[" + lbl + "]");
 
471
 
 
472
                        break;
 
473
 
 
474
                case GeoElement.GEO_CLASS_FUNCTION:
 
475
 
 
476
                        double x = start;
 
477
                        for(int i = 0; i < 10; i++){
 
478
                                property.add("" + x);
 
479
                                value.add( lbl + "(" + x + ")");
 
480
                                x = x + step;
 
481
                        }
 
482
 
 
483
                        break;
 
484
 
 
485
                }
 
486
 
 
487
                boolean isFunction = selectedGeo.getGeoClassType() == GeoElement.GEO_CLASS_FUNCTION;
 
488
 
 
489
                int rowCount = Math.max(minRows, property.size());
 
490
                int columnCount = isFunction ?  2 + extraColumnList.size() : 2;
 
491
 
 
492
                model = new DefaultTableModel(rowCount, columnCount);
 
493
 
 
494
                for(int i=0; i < property.size(); i++){
 
495
                        model.setValueAt(property.get(i),i,0);
 
496
                        model.setValueAt(evaluateToText("\"\"" + value.get(i)),i,1);
 
497
                }
 
498
 
 
499
                if(isFunction){
 
500
                        updateExtraColumns();
 
501
                }
 
502
 
 
503
                table.setModel(model);
 
504
 
 
505
 
 
506
                if(isFunction){
 
507
 
 
508
                        table.getColumnModel().getColumn(0).setHeaderValue("x");
 
509
                        table.getColumnModel().getColumn(1).setHeaderValue("y");
 
510
                        for(int i = 0; i < extraColumnList.size() ; i++){                               
 
511
                                table.getColumnModel().getColumn(i+2).setHeaderValue(columnNames[extraColumnList.get(i)]);
 
512
 
 
513
                        }
 
514
                        controlPanel.setVisible(true);
 
515
 
 
516
                }else{
 
517
                        table.getColumnModel().getColumn(0).setHeaderValue(app.getPlain("Property"));
 
518
                        table.getColumnModel().getColumn(1).setHeaderValue(app.getPlain("Value"));
 
519
 
 
520
                        controlPanel.setVisible(false);
 
521
                }
 
522
 
 
523
                //setColumnWidths();
 
524
        }
 
525
 
 
526
 
 
527
 
 
528
 
 
529
        private void addColumn(int columnType){
 
530
                extraColumnList.add(columnType);
 
531
                populateTableModel();
 
532
        }
 
533
 
 
534
        private void removeColumn(){
 
535
                int count = table.getColumnCount();
 
536
                if(count <= 2) return;
 
537
                extraColumnList.remove(extraColumnList.size()-1);
 
538
                populateTableModel();
 
539
 
 
540
        }
 
541
 
 
542
 
 
543
        private void updateExtraColumns(){
 
544
 
 
545
                if(extraColumnList.size()==0) return;
 
546
 
 
547
                String expr;
 
548
 
 
549
                for(int column = 2; column < extraColumnList.size() + 2; column ++ ){
 
550
 
 
551
                        int columnType = extraColumnList.get(column-2);
 
552
                        switch (columnType){
 
553
 
 
554
                        case COL_DERIVATIVE:
 
555
 
 
556
                                for(int row=0; row < table.getRowCount(); row++){
 
557
                                        double x = Double.parseDouble((String) model.getValueAt(row, 0));
 
558
                                        expr = derivative.getLabel() + "(" + x + ")";
 
559
                                        model.setValueAt(evaluateToText("\"\"" + expr),row,column);
 
560
                                }       
 
561
                                break;
 
562
 
 
563
                        case COL_DERIVATIVE2:
 
564
 
 
565
                                for(int row=0; row < table.getRowCount(); row++){
 
566
                                        double x = Double.parseDouble((String) model.getValueAt(row, 0));
 
567
                                        expr = derivative2.getLabel() + "(" + x + ")";
 
568
                                        model.setValueAt(evaluateToText("\"\"" + expr),row,column);
 
569
                                }       
 
570
                                break;
 
571
 
 
572
                        case COL_CURVATURE:
 
573
 
 
574
                                for(int row=0; row < table.getRowCount(); row++){
 
575
                                        double x = Double.parseDouble((String) model.getValueAt(row, 0));
 
576
                                        double y = Double.parseDouble((String) model.getValueAt(row, 1));
 
577
                                        double c = this.evaluateExpression(
 
578
                                                        "Curvature[ (" + x + "," + y  + ")," + selectedGeo.getLabel() + "]");
 
579
                                        model.setValueAt("" + c,row,column);
 
580
                                }       
 
581
                                break;
 
582
 
 
583
                        case COL_DIFFERENCE:
 
584
 
 
585
                                for(int row=1; row < table.getRowCount(); row++){
 
586
                                        if(model.getValueAt(row-1, column -1) != null){
 
587
                                                double prev = Double.parseDouble((String) model.getValueAt(row-1, column -1));
 
588
                                                double x = Double.parseDouble((String) model.getValueAt(row, column-1));
 
589
                                                model.setValueAt("" + (x - prev),row,column);
 
590
                                        }else{
 
591
                                                model.setValueAt(null,row,column);
 
592
                                        }
 
593
                                }       
 
594
                                break;
 
595
 
 
596
                        }
 
597
 
 
598
                }
 
599
 
 
600
        }
 
601
 
 
602
 
 
603
 
 
604
        private String getTitleString(){
 
605
 
 
606
                String title;
 
607
 
 
608
                if(selectedGeo == null){
 
609
                        title = app.getMenu("SelectObject");
 
610
 
 
611
                }else{
 
612
                        //      title = selectedGeo.getLongDescriptionHTML(false, true);
 
613
                        //      if (title.length() > 80)
 
614
                        title = selectedGeo.getNameDescriptionHTML(false, true);          
 
615
                }
 
616
                return title;
 
617
        }
 
618
 
 
619
 
 
620
 
 
621
        private void setColumnWidths(){
 
622
 
 
623
                int w;
 
624
                for (int i = 0; i < table.getColumnCount(); ++ i) {     
 
625
                        w = getMaxColumnWidth(table,i) + 5; 
 
626
                        table.getColumnModel().getColumn(i).setPreferredWidth(w);
 
627
                }
 
628
 
 
629
                int gap = table.getParent().getPreferredSize().width - table.getPreferredSize().width;
 
630
                //System.out.println(table.getParent().getPreferredSize().width);
 
631
                if(gap > 0){
 
632
                        w = table.getColumnCount() - 1;
 
633
                        int newWidth = gap + table.getColumnModel().getColumn(table.getColumnCount() - 1).getWidth() ;
 
634
                        table.getColumnModel().getColumn(w).setPreferredWidth(newWidth);
 
635
                }
 
636
        }
 
637
 
 
638
 
 
639
        /**
 
640
         * Finds the maximum preferred width of a column.
 
641
         */
 
642
        public int getMaxColumnWidth(JTable table, int column){
 
643
 
 
644
                TableColumn tableColumn = table.getColumnModel().getColumn(column); 
 
645
 
 
646
                // iterate through the rows and find the preferred width
 
647
                int maxPrefWidth = tableColumn.getPreferredWidth();
 
648
                int colPrefWidth = 0;
 
649
                for (int row = 0; row < table.getRowCount(); row++) {
 
650
                        if(table.getValueAt(row, column)!=null){
 
651
                                colPrefWidth = (int) table.getCellRenderer(row, column)
 
652
                                .getTableCellRendererComponent(table,
 
653
                                                table.getValueAt(row, column), false, false,
 
654
                                                row, column).getPreferredSize().getWidth();
 
655
                                maxPrefWidth = Math.max(maxPrefWidth, colPrefWidth);
 
656
                        }
 
657
                }
 
658
 
 
659
                return maxPrefWidth + table.getIntercellSpacing().width;
 
660
        }
 
661
 
 
662
 
 
663
 
 
664
 
 
665
 
 
666
        //  Action and Other Event Handlers
 
667
        // =====================================
 
668
 
 
669
        public void actionPerformed(ActionEvent e) {    
 
670
                Object source = e.getSource();
 
671
 
 
672
                if (source instanceof JTextField) {
 
673
                        doTextFieldActionPerformed((JTextField)source);
 
674
                }
 
675
 
 
676
                
 
677
                else if (source == btnAddColumn) {
 
678
                        //System.out.println(btnAddColumn.getSelectedIndex() + "=============");
 
679
                        addColumn(btnAddColumn.getSelectedIndex());
 
680
                }       
 
681
 
 
682
                
 
683
                else if (source == btnAdd) {
 
684
                        addColumn(cbAdd.getSelectedIndex());
 
685
                }       
 
686
 
 
687
                else if (source == btnRemove) {
 
688
                        removeColumn();
 
689
                }       
 
690
 
 
691
                else if (source == ckShowTangent) {
 
692
                        tangentLine.setEuclidianVisible(ckShowTangent.isSelected());
 
693
                        tangentLine.updateRepaint();
 
694
                }       
 
695
 
 
696
                else if (source == ckShowOscCircle) {
 
697
                        oscCircle.setEuclidianVisible(ckShowOscCircle.isSelected());
 
698
                        oscCircle.updateRepaint();
 
699
                }       
 
700
 
 
701
                else if (source == ckShowX) {
 
702
                        xSegment.setEuclidianVisible(ckShowX.isSelected());
 
703
                        xSegment.updateRepaint();
 
704
                }       
 
705
 
 
706
                else if (source == ckShowY) {
 
707
                        ySegment.setEuclidianVisible(ckShowY.isSelected());
 
708
                        ySegment.updateRepaint();
 
709
                }       
 
710
 
 
711
 
 
712
 
 
713
 
 
714
 
 
715
        }       
 
716
 
 
717
        private void doTextFieldActionPerformed(JTextField source) {
 
718
                try {
 
719
                        String inputText = source.getText().trim();
 
720
                        Double value = Double.parseDouble(source.getText());
 
721
 
 
722
                        if (value != null) {
 
723
                                if (source == fldStep) 
 
724
                                        step = value;   
 
725
                                if (source == fldStart)
 
726
                                        start = value;
 
727
                                this.populateTableModel();                      
 
728
                        }
 
729
                } catch (NumberFormatException e) {
 
730
                        e.printStackTrace();
 
731
                }
 
732
 
 
733
        }
 
734
 
 
735
 
 
736
 
 
737
        public void setVisible(boolean isVisible) {             
 
738
                super.setVisible(isVisible);
 
739
 
 
740
                if (isVisible) {
 
741
                        app.getKernel().attach(this);
 
742
                } else {
 
743
                        app.getKernel().detach(this);
 
744
                        clearGeoList();
 
745
                }               
 
746
        }
 
747
 
 
748
 
 
749
 
 
750
        //     View Implementation
 
751
        // =====================================
 
752
 
 
753
        public void update(GeoElement geo) {
 
754
                if(selectedGeo == null) return;
 
755
                if(selectedGeo.equals(geo)){
 
756
                        lblGeoName.setText(selectedGeo.toString());
 
757
                        populateTableModel();
 
758
                        table.repaint();
 
759
                }
 
760
        }
 
761
 
 
762
        public void add(GeoElement geo) {}
 
763
        public void remove(GeoElement geo) {}
 
764
        public void rename(GeoElement geo) {}
 
765
        public void updateAuxiliaryObject(GeoElement geo) {}
 
766
        public void repaintView() {}
 
767
        public void reset() {}
 
768
        public void clearView() {}
 
769
        public void setMode(int mode) {}
 
770
 
 
771
 
 
772
 
 
773
        //  Table Selection Listener
 
774
        // =====================================
 
775
 
 
776
        public void valueChanged(ListSelectionEvent e) {
 
777
 
 
778
                if (e.getValueIsAdjusting()) return;
 
779
 
 
780
                table.getSelectionModel().removeListSelectionListener(this);
 
781
                if (e.getSource() == table.getSelectionModel() && table.getRowSelectionAllowed()) {
 
782
                        // row selection changed
 
783
                        updateTestPoint();
 
784
                }               
 
785
                table.getSelectionModel().addListSelectionListener(this);
 
786
        }
 
787
 
 
788
 
 
789
 
 
790
        //    Geo Selection Listener
 
791
        // =====================================
 
792
 
 
793
        public void geoElementSelected(GeoElement geo, boolean addToSelection) {
 
794
                // TODO: not working directly yet, currently the listener
 
795
                // is in InputDialog, so an overridden insertGeoElement() is used instead
 
796
        }
 
797
        public void insertGeoElement(GeoElement geo) {
 
798
                selectedGeo = geo;
 
799
                lblGeoName.setText(getTitleString());
 
800
 
 
801
                if(selectedGeo.getGeoClassType() == GeoElement.GEO_CLASS_FUNCTION){
 
802
                        
 
803
                        start = 0.5* (kernel.getApplication().getEuclidianView().getXmin()-
 
804
                        kernel.getApplication().getEuclidianView().getXmin());
 
805
                        step = 0.25 * kernel.getApplication().getEuclidianView().getGridDistances()[0];
 
806
                        fldStart.removeActionListener(this);
 
807
                        fldStep.removeActionListener(this);
 
808
                        fldStart.setText("" + start);
 
809
                        fldStep.setText("" + step);
 
810
                        fldStart.addActionListener(this);
 
811
                        fldStep.addActionListener(this);
 
812
 
 
813
                        defineDisplayGeos();
 
814
                }
 
815
 
 
816
                populateTableModel();
 
817
                this.pack();
 
818
                //table.changeSelection(0,0, false, false);
 
819
 
 
820
 
 
821
        }
 
822
 
 
823
 
 
824
 
 
825
 
 
826
        //      Key Listeners
 
827
        //=========================================
 
828
 
 
829
        public void keyPressed(KeyEvent e) {
 
830
 
 
831
                int key = e.getKeyCode();
 
832
 
 
833
                table.getSelectionModel().removeListSelectionListener(this);
 
834
                switch (key){
 
835
                case KeyEvent.VK_UP:
 
836
                        if(table.getSelectedRow()==0){
 
837
                                start = start-step;
 
838
                                populateTableModel();
 
839
                                updateTestPoint();
 
840
                        }
 
841
                        break;
 
842
 
 
843
                case KeyEvent.VK_DOWN:
 
844
                        if(table.getSelectedRow()==table.getRowCount()-1){
 
845
                                start = start+step;
 
846
                                populateTableModel();
 
847
                                table.changeSelection(table.getRowCount()-1, 0, false, false);
 
848
                                updateTestPoint();
 
849
                        }
 
850
                        break;
 
851
                }
 
852
 
 
853
                table.getSelectionModel().addListSelectionListener(this);
 
854
 
 
855
        }
 
856
 
 
857
        public void keyReleased(KeyEvent arg0) {}
 
858
        public void keyTyped(KeyEvent arg0) {}
 
859
 
 
860
 
 
861
 
 
862
 
 
863
        //      Mouse Listeners
 
864
        //=========================================
 
865
 
 
866
        public void mouseClicked(MouseEvent arg0) { }
 
867
        public void mouseEntered(MouseEvent arg0) { }
 
868
        public void mouseExited(MouseEvent arg0) { }
 
869
        public void mousePressed(MouseEvent arg0) { }
 
870
        public void mouseReleased(MouseEvent arg0) { }
 
871
 
 
872
 
 
873
 
 
874
 
 
875
 
 
876
        //  Update/Create Display Geos
 
877
        //=========================================
 
878
 
 
879
        private void defineDisplayGeos(){
 
880
 
 
881
                // test point
 
882
                if(testPoint != null) 
 
883
                        testPoint.remove();
 
884
                String expr = "Point[" + selectedGeo.getLabel() + "]";
 
885
                testPoint = (GeoPoint) createGeoFromString(expr, null, true);
 
886
                testPoint.setObjColor(Color.red);
 
887
                testPoint.setPointSize(4);
 
888
                testPoint.setLabel("testPoint");
 
889
 
 
890
                // X segment
 
891
                if(xSegment != null) 
 
892
                        xSegment.remove();
 
893
 
 
894
                expr = "Segment[" + testPoint.getLabel() + ", (x(" + testPoint.getLabel() + "),0) ]";
 
895
                //Application.debug(expr);
 
896
                xSegment = createGeoFromString(expr, null, true);
 
897
                xSegment.setEuclidianVisible(true);
 
898
                xSegment.setObjColor(Color.red);
 
899
                xSegment.setLabel("xSegment");
 
900
 
 
901
                // Y segment
 
902
                if(ySegment != null) 
 
903
                        ySegment.remove();
 
904
 
 
905
                expr = "Segment[" + testPoint.getLabel() + ", (0, y(" + testPoint.getLabel() + ")) ]";
 
906
                //Application.debug(expr);
 
907
                ySegment = createGeoFromString(expr, null, true);
 
908
                ySegment.setObjColor(Color.red);
 
909
                ySegment.setEuclidianVisible(true);
 
910
                ySegment.setLabel("ySegment");
 
911
 
 
912
 
 
913
                // tangent line
 
914
                if(tangentLine != null) 
 
915
                        tangentLine.remove();
 
916
 
 
917
                expr = "Tangent[" + selectedGeo.getLabel() + "," + testPoint.getLabel() + "]";
 
918
                //Application.debug(expr);
 
919
                tangentLine = createGeoFromString(expr, null, true);
 
920
                tangentLine.setObjColor(Color.red);
 
921
                tangentLine.setEuclidianVisible(false);
 
922
                tangentLine.setLabel("tangentLine");
 
923
 
 
924
 
 
925
                // osculating circle
 
926
                if( oscCircle != null) 
 
927
                        oscCircle.remove();
 
928
 
 
929
                expr = "OsculatingCircle[" + testPoint.getLabel() + "," + selectedGeo.getLabel() + "]";
 
930
                //Application.debug(expr);
 
931
                oscCircle = createGeoFromString(expr, null, true);
 
932
                oscCircle.setObjColor(Color.red);
 
933
                oscCircle.setEuclidianVisible(false);
 
934
                oscCircle.setLabel("oscCircle");
 
935
 
 
936
 
 
937
                // derivative
 
938
                if( derivative != null) 
 
939
                        derivative.remove();
 
940
 
 
941
                expr = "Derivative[" + selectedGeo.getLabel() + "]";
 
942
                //Application.debug(expr);
 
943
                derivative = createGeoFromString(expr, null, true);
 
944
                derivative.setEuclidianVisible(false);
 
945
                derivative.setLabel("derivative");
 
946
 
 
947
                // 2nd derivative
 
948
                if( derivative2 != null) 
 
949
                        derivative2.remove();
 
950
 
 
951
                expr = "Derivative[" + selectedGeo.getLabel() + " , 2 ]";
 
952
                //Application.debug(expr);
 
953
                derivative2 = createGeoFromString(expr, null, true);
 
954
                derivative2.setEuclidianVisible(false);
 
955
                derivative2.setLabel("derivative2");
 
956
 
 
957
                updateTestPoint();
 
958
 
 
959
        }
 
960
 
 
961
 
 
962
 
 
963
        private void updateTestPoint(){
 
964
 
 
965
                if(testPoint != null){
 
966
                        int row = table.getSelectedRow();
 
967
                        if (row >=0){
 
968
                                double x = Double.parseDouble((String) model.getValueAt(row, 0));
 
969
                                double y = Double.parseDouble((String) model.getValueAt(row, 1));
 
970
                                testPoint.setCoords(x, y, 1);
 
971
                                testPoint.updateRepaint();      
 
972
                        }
 
973
                }
 
974
        }
 
975
 
 
976
 
 
977
 
 
978
 
 
979
 
 
980
        //  Geo Creation and Evaluation Methods
 
981
        //=========================================
 
982
 
 
983
        private double evaluateExpression(String expr){
 
984
                NumberValue nv;
 
985
                nv = kernel.getAlgebraProcessor().evaluateToNumeric(expr, false);       
 
986
                return nv.getDouble();
 
987
        }
 
988
 
 
989
 
 
990
        private String evaluateToText(String expr){
 
991
                GeoText text = kernel.getAlgebraProcessor().evaluateToText(expr, false);        
 
992
                return text.getTextString();
 
993
        }
 
994
 
 
995
 
 
996
        public GeoElement createGeoFromString(String text, String label, boolean suppressLabelCreation ){
 
997
 
 
998
                try {
 
999
 
 
1000
                        boolean oldSuppressLabelMode = cons.isSuppressLabelsActive();
 
1001
 
 
1002
                        if(suppressLabelCreation)
 
1003
                                cons.setSuppressLabelCreation(true);
 
1004
                        //Application.debug(text);
 
1005
                        GeoElement[] geos = kernel.getAlgebraProcessor()
 
1006
                        .processAlgebraCommandNoExceptions(text, false);        
 
1007
 
 
1008
                        if(label != null)
 
1009
                                geos[0].setLabel(label);
 
1010
 
 
1011
                        // set visibility
 
1012
                        geos[0].setEuclidianVisible(true);      
 
1013
                        geos[0].setAuxiliaryObject(true);
 
1014
                        geos[0].setLabelVisible(false);
 
1015
 
 
1016
                        if(suppressLabelCreation)
 
1017
                                cons.setSuppressLabelCreation(oldSuppressLabelMode);
 
1018
 
 
1019
                        geoList.add(geos[0]);
 
1020
                        return geos[0];
 
1021
 
 
1022
                } catch (Exception e) {
 
1023
                        e.printStackTrace();
 
1024
                        return null;
 
1025
                }
 
1026
        }
 
1027
 
 
1028
 
 
1029
 
 
1030
        private void clearGeoList(){
 
1031
                for(GeoElement geo : geoList){
 
1032
                        if(geo != null)
 
1033
                                geo.remove();
 
1034
                }
 
1035
                geoList.clear();
 
1036
        }
 
1037
 
 
1038
        public void updateFont(){
 
1039
                this.setFont(app.getPlainFont());
 
1040
        }
 
1041
 
 
1042
 
 
1043
 
 
1044
        public int getViewID() {
 
1045
                return Application.VIEW_INSPECTOR;
 
1046
        }
 
1047
 
 
1048
 
 
1049
}
 
1050
 
 
1051
 
 
1052
 
 
1053
 
 
1054
 
 
1055