~typically/modelgui/mgui-core

« back to all changes in this revision

Viewing changes to src/mgui/io/domestic/shapes/ShapeInputDialogBox.java

  • Committer: Andrew Reid
  • Date: 2015-09-23 18:21:45 UTC
  • Revision ID: typically@yahoo.com-20150923182145-xhe8y09h96m4pxej
Added new source files XML, etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* Copyright (C) 2014 Andrew Reid and the ModelGUI Project <http://mgui.wikidot.com>
 
3
 
4
* This file is part of ModelGUI[core] (mgui-core).
 
5
 
6
* ModelGUI[core] is free software: you can redistribute it and/or modify
 
7
* it under the terms of the GNU General Public License as published by
 
8
* the Free Software Foundation, either version 3 of the License, or
 
9
* (at your option) any later version.
 
10
 
11
* ModelGUI[core] is distributed in the hope that it will be useful,
 
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
* GNU General Public License for more details.
 
15
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with ModelGUI[core]. If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
package mgui.io.domestic.shapes;
 
21
 
 
22
import java.awt.event.ActionEvent;
 
23
import java.io.File;
 
24
import java.util.Vector;
 
25
 
 
26
import javax.swing.JFrame;
 
27
import javax.swing.JLabel;
 
28
import javax.swing.JScrollPane;
 
29
import javax.swing.JTable;
 
30
import javax.swing.table.AbstractTableModel;
 
31
 
 
32
import mgui.interfaces.InterfaceEnvironment;
 
33
import mgui.interfaces.InterfaceSession;
 
34
import mgui.interfaces.gui.InterfaceComboBox;
 
35
import mgui.interfaces.io.InterfaceIODialogBox;
 
36
import mgui.interfaces.layouts.LineLayout;
 
37
import mgui.interfaces.layouts.LineLayoutConstraints;
 
38
import mgui.interfaces.shapes.ShapeSet3DInt;
 
39
import mgui.io.InterfaceIOOptions;
 
40
import mgui.io.InterfaceIOPanel;
 
41
 
 
42
/*********************************************
 
43
 * Dialog box for loading a ModelGUI shape. The default implementation is for XML encoding. 
 
44
 * 
 
45
 * @author Andrew Reid
 
46
 * @version 1.0
 
47
 * @since 1.0
 
48
 *
 
49
 */
 
50
public class ShapeInputDialogBox extends InterfaceIODialogBox {
 
51
 
 
52
        protected JLabel lblShapeSet = new JLabel("Add to shape set:");
 
53
        protected InterfaceComboBox cmbShapeSet = new InterfaceComboBox(InterfaceComboBox.RenderMode.LongestItem, 
 
54
                          true, 500);
 
55
        
 
56
        
 
57
protected JLabel lblSurfaces = new JLabel("Surfaces:");
 
58
        
 
59
        protected JTable table;
 
60
        protected JScrollPane scrColumns;
 
61
        
 
62
        protected LineLayout lineLayout;
 
63
        
 
64
        protected int table_offset = 0;
 
65
        
 
66
        public ShapeInputDialogBox(){
 
67
                super();
 
68
                init();
 
69
        }
 
70
        
 
71
        public ShapeInputDialogBox(JFrame frame, InterfaceIOPanel panel, InterfaceIOOptions options){
 
72
                super(frame, panel, options);
 
73
                ShapeInputOptions _options = (ShapeInputOptions)options;
 
74
                if (_options.shape_set == null)
 
75
                        _options.shape_set = InterfaceSession.getDisplayPanel().getCurrentShapeSet();
 
76
                init();
 
77
        }
 
78
        
 
79
        @Override
 
80
        protected void init(){
 
81
                super.init();
 
82
                
 
83
                this.setDialogSize(570,400);
 
84
                this.setTitle("Surface Input Options");
 
85
                
 
86
                lineLayout = new LineLayout(InterfaceEnvironment.getLineHeight(), 5, 0);
 
87
                this.setMainLayout(lineLayout);
 
88
                
 
89
                updateShapeSets();
 
90
                
 
91
                LineLayoutConstraints c = new LineLayoutConstraints(1, 1, 0.05, 0.3, 1);
 
92
                mainPanel.add(lblShapeSet, c);
 
93
                c = new LineLayoutConstraints(1, 1, 0.35, 0.6, 1);
 
94
                mainPanel.add(cmbShapeSet, c);
 
95
                c = new LineLayoutConstraints(2, 2, 0.05, 0.9, 1);
 
96
                mainPanel.add(lblSurfaces, c);
 
97
                
 
98
                updateTable();
 
99
                
 
100
        }
 
101
        
 
102
        void updateShapeSets(){
 
103
                
 
104
                if (!InterfaceSession.isInit()) return;
 
105
                ShapeSet3DInt current_set = (ShapeSet3DInt)cmbShapeSet.getSelectedItem();
 
106
                ShapeSet3DInt shape_set = new ShapeSet3DInt();
 
107
                InterfaceSession.getWorkspace().populateShapeTypeCombo(cmbShapeSet, shape_set);
 
108
                
 
109
                if (current_set != null)
 
110
                        cmbShapeSet.setSelectedItem(current_set);
 
111
        }
 
112
        
 
113
        @Override
 
114
        public void showDialog(){
 
115
                ShapeInputOptions _options = (ShapeInputOptions)options;
 
116
                if (_options == null) _options = new ShapeInputOptions();
 
117
                if (_options.shape_set == null)
 
118
                        _options.shape_set = InterfaceSession.getDisplayPanel().getCurrentShapeSet();
 
119
                
 
120
                updateShapeSets();
 
121
                if (_options.shape_set != null)
 
122
                        cmbShapeSet.setSelectedItem(_options.shape_set);
 
123
                
 
124
                updateTable();
 
125
                setVisible(true);
 
126
        }
 
127
        
 
128
        protected void updateTable(){
 
129
                
 
130
                if (options == null) return;
 
131
                ShapeInputOptions _options = (ShapeInputOptions)options;
 
132
                
 
133
                //header and new table model
 
134
                if (scrColumns != null) mainPanel.remove(scrColumns);
 
135
                
 
136
                if (_options.getFiles() == null){
 
137
                        lblSurfaces.setVisible(false);
 
138
                        return;
 
139
                }
 
140
                
 
141
                lblSurfaces.setVisible(true);
 
142
                
 
143
                Vector<String> v_files = new Vector<String>();
 
144
                Vector<String> v_names = new Vector<String>();
 
145
                
 
146
                File[] files = options.getFiles();
 
147
                String[] names = _options.names;
 
148
                
 
149
                for (int i = 0; i < files.length; i++){
 
150
                        v_files.add(files[i].getName());
 
151
                        v_names.add(names[i]);
 
152
                        }
 
153
                
 
154
                Vector<Vector> values = new Vector<Vector>(files.length);
 
155
                for (int i = 0; i < files.length; i++){
 
156
                        Vector v = new Vector(4);
 
157
                        v.add(v_names.get(i));
 
158
                        v.add(v_files.get(i));
 
159
                        values.add(v);
 
160
                        }
 
161
                
 
162
                Vector<String> header = new Vector<String>(4);
 
163
                header.add("Name");
 
164
                header.add("Filename");
 
165
                
 
166
                TableModel model = new TableModel(values, header);
 
167
                table = new JTable(model);
 
168
                scrColumns = new JScrollPane(table);
 
169
                table.getColumnModel().getColumn(0).setPreferredWidth(15);
 
170
                
 
171
                LineLayoutConstraints c = new LineLayoutConstraints(4 + table_offset, 8 + table_offset, 0.05, 0.9, 1);
 
172
                
 
173
                mainPanel.add(scrColumns, c);
 
174
                lineLayout.setFlexibleComponent(scrColumns);
 
175
                mainPanel.updateUI();
 
176
                
 
177
        }
 
178
        
 
179
        @Override
 
180
        public void actionPerformed(ActionEvent e){
 
181
                
 
182
                
 
183
                if (e.getActionCommand().equals(DLG_CMD_OK)){
 
184
                        
 
185
                        ShapeInputOptions _options = (ShapeInputOptions)options;
 
186
                
 
187
                        if (table == null){
 
188
                                setVisible(false);
 
189
                                return;
 
190
                                }
 
191
                        
 
192
                        for (int i = 0; i < table.getModel().getRowCount(); i++)
 
193
                                _options.names[i] = (String)table.getValueAt(i, 0);
 
194
                        
 
195
                        _options.shape_set = (ShapeSet3DInt)cmbShapeSet.getSelectedItem();
 
196
                                
 
197
                        setVisible(false);
 
198
                        return;
 
199
                        }
 
200
                
 
201
                super.actionPerformed(e);
 
202
                
 
203
        }
 
204
        
 
205
        protected class TableModel extends AbstractTableModel {
 
206
              
 
207
                Vector<Vector> data;
 
208
                Vector<String> columns;
 
209
                
 
210
                public TableModel(Vector<Vector> data, Vector<String> columns){
 
211
                        this.data = data;
 
212
                        this.columns = columns;
 
213
                }
 
214
 
 
215
        public int getColumnCount() {
 
216
            return columns.size();
 
217
        }
 
218
 
 
219
        public int getRowCount() {
 
220
            return data.size();
 
221
        }
 
222
 
 
223
        @Override
 
224
                public String getColumnName(int col) {
 
225
            return columns.get(col);
 
226
        }
 
227
 
 
228
        public Object getValueAt(int row, int col) {
 
229
            return data.get(row).get(col);
 
230
        }
 
231
 
 
232
        @Override
 
233
                public Class getColumnClass(int c) {
 
234
            return getValueAt(0, c).getClass();
 
235
        }
 
236
 
 
237
        /*
 
238
         * Column names not editable
 
239
         */
 
240
        @Override
 
241
                public boolean isCellEditable(int row, int col) {
 
242
                if (col == 1) return false;
 
243
                return true;
 
244
        }
 
245
 
 
246
        @Override
 
247
                public void setValueAt(Object value, int row, int col) {
 
248
            data.get(row).set(col, value);
 
249
            fireTableCellUpdated(row, col);
 
250
        }
 
251
 
 
252
    }
 
253
        
 
254
        
 
255
}