001    /*
002     * Copyright 2009 - 2010 JEuclid, http://jeuclid.sf.net
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id $ */
018    
019    package net.sourceforge.jeuclid.app.mathviewer;
020    
021    // CHECKSTYLE:OFF
022    public class InsertTableDialog extends javax.swing.JDialog {
023        // CHECKSTYLE:OFF
024        private String mathMLText;
025    
026        /** Creates new form InsertTableDialog */
027        public InsertTableDialog(final java.awt.Frame parent, final boolean modal) {
028            super(parent, modal);
029            this.initComponents();
030        }
031    
032        /**
033         * This method is called from within the constructor to initialize the form.
034         * WARNING: Do NOT modify this code. The content of this method is always
035         * regenerated by the Form Editor.
036         */
037        @SuppressWarnings("unchecked")
038        // <editor-fold defaultstate="collapsed"
039        // desc="Generated Code">//GEN-BEGIN:initComponents
040        private void initComponents() {
041    
042            this.buttonGroupType = new javax.swing.ButtonGroup();
043            this.tableRadioButton = new javax.swing.JRadioButton();
044            this.matrixRadioButton = new javax.swing.JRadioButton();
045            this.determinantRadioButton = new javax.swing.JRadioButton();
046            this.cancelButton = new javax.swing.JButton();
047            this.okButton = new javax.swing.JButton();
048            this.columnsLabel = new javax.swing.JLabel();
049            this.rowsSpinner = new javax.swing.JSpinner();
050            this.columnsSpinner = new javax.swing.JSpinner();
051            this.typeLabel = new javax.swing.JLabel();
052            this.rowsLabel = new javax.swing.JLabel();
053    
054            this
055                    .setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
056    
057            this.buttonGroupType.add(this.tableRadioButton);
058            this.tableRadioButton.setText("Tabelle");
059    
060            this.buttonGroupType.add(this.matrixRadioButton);
061            this.matrixRadioButton.setSelected(true);
062            this.matrixRadioButton.setText("Matrix");
063    
064            this.buttonGroupType.add(this.determinantRadioButton);
065            this.determinantRadioButton.setText("Determinante");
066    
067            this.cancelButton.setText("Abbrechen");
068            this.cancelButton
069                    .addActionListener(new java.awt.event.ActionListener() {
070                        public void actionPerformed(
071                                final java.awt.event.ActionEvent evt) {
072                            InsertTableDialog.this.cancelButtonActionPerformed(evt);
073                        }
074                    });
075    
076            this.okButton.setText("OK");
077            this.okButton.addActionListener(new java.awt.event.ActionListener() {
078                public void actionPerformed(final java.awt.event.ActionEvent evt) {
079                    InsertTableDialog.this.okButtonActionPerformed(evt);
080                }
081            });
082    
083            this.columnsLabel.setText("Spalten");
084    
085            this.rowsSpinner.setModel(new javax.swing.SpinnerNumberModel(3, 1, 99,
086                    1));
087    
088            this.columnsSpinner.setModel(new javax.swing.SpinnerNumberModel(3, 1,
089                    99, 1));
090    
091            this.typeLabel.setText("Typ");
092    
093            this.rowsLabel.setText("Zeilen");
094    
095            final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this
096                    .getContentPane());
097            this.getContentPane().setLayout(layout);
098            layout
099                    .setHorizontalGroup(layout
100                            .createParallelGroup(
101                                    javax.swing.GroupLayout.Alignment.LEADING)
102                            .addGroup(
103                                    layout
104                                            .createSequentialGroup()
105                                            .addGroup(
106                                                    layout
107                                                            .createParallelGroup(
108                                                                    javax.swing.GroupLayout.Alignment.LEADING)
109                                                            .addGroup(
110                                                                    layout
111                                                                            .createSequentialGroup()
112                                                                            .addGap(
113                                                                                    24,
114                                                                                    24,
115                                                                                    24)
116                                                                            .addComponent(
117                                                                                    this.typeLabel)
118                                                                            .addGap(
119                                                                                    18,
120                                                                                    18,
121                                                                                    18)
122                                                                            .addGroup(
123                                                                                    layout
124                                                                                            .createParallelGroup(
125                                                                                                    javax.swing.GroupLayout.Alignment.LEADING)
126                                                                                            .addComponent(
127                                                                                                    this.tableRadioButton)
128                                                                                            .addComponent(
129                                                                                                    this.matrixRadioButton)
130                                                                                            .addComponent(
131                                                                                                    this.determinantRadioButton)))
132                                                            .addGroup(
133                                                                    layout
134                                                                            .createSequentialGroup()
135                                                                            .addGap(
136                                                                                    24,
137                                                                                    24,
138                                                                                    24)
139                                                                            .addComponent(
140                                                                                    this.rowsLabel)
141                                                                            .addGap(
142                                                                                    14,
143                                                                                    14,
144                                                                                    14)
145                                                                            .addComponent(
146                                                                                    this.rowsSpinner,
147                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
148                                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
149                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
150                                                                            .addGap(
151                                                                                    18,
152                                                                                    18,
153                                                                                    18)
154                                                                            .addComponent(
155                                                                                    this.columnsLabel)
156                                                                            .addPreferredGap(
157                                                                                    javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
158                                                                            .addComponent(
159                                                                                    this.columnsSpinner,
160                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
161                                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
162                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE))
163                                                            .addGroup(
164                                                                    layout
165                                                                            .createSequentialGroup()
166                                                                            .addGap(
167                                                                                    39,
168                                                                                    39,
169                                                                                    39)
170                                                                            .addComponent(
171                                                                                    this.okButton,
172                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
173                                                                                    93,
174                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
175                                                                            .addPreferredGap(
176                                                                                    javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
177                                                                            .addComponent(
178                                                                                    this.cancelButton)))
179                                            .addContainerGap(44, Short.MAX_VALUE)));
180            layout
181                    .setVerticalGroup(layout
182                            .createParallelGroup(
183                                    javax.swing.GroupLayout.Alignment.LEADING)
184                            .addGroup(
185                                    layout
186                                            .createSequentialGroup()
187                                            .addGap(25, 25, 25)
188                                            .addGroup(
189                                                    layout
190                                                            .createParallelGroup(
191                                                                    javax.swing.GroupLayout.Alignment.BASELINE)
192                                                            .addComponent(
193                                                                    this.rowsLabel)
194                                                            .addComponent(
195                                                                    this.columnsLabel)
196                                                            .addComponent(
197                                                                    this.rowsSpinner,
198                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
199                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
200                                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
201                                                            .addComponent(
202                                                                    this.columnsSpinner,
203                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
204                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
205                                                                    javax.swing.GroupLayout.PREFERRED_SIZE))
206                                            .addGap(18, 18, 18)
207                                            .addGroup(
208                                                    layout
209                                                            .createParallelGroup(
210                                                                    javax.swing.GroupLayout.Alignment.BASELINE)
211                                                            .addComponent(
212                                                                    this.typeLabel)
213                                                            .addComponent(
214                                                                    this.matrixRadioButton))
215                                            .addPreferredGap(
216                                                    javax.swing.LayoutStyle.ComponentPlacement.RELATED)
217                                            .addComponent(this.tableRadioButton)
218                                            .addPreferredGap(
219                                                    javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
220                                            .addComponent(
221                                                    this.determinantRadioButton)
222                                            .addGap(18, 18, 18)
223                                            .addGroup(
224                                                    layout
225                                                            .createParallelGroup(
226                                                                    javax.swing.GroupLayout.Alignment.BASELINE)
227                                                            .addComponent(
228                                                                    this.cancelButton)
229                                                            .addComponent(
230                                                                    this.okButton))
231                                            .addContainerGap(
232                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
233                                                    Short.MAX_VALUE)));
234    
235            this.pack();
236        }// </editor-fold>//GEN-END:initComponents
237    
238        private void cancelButtonActionPerformed(
239                final java.awt.event.ActionEvent evt) {// GEN-FIRST:event_cancelButtonActionPerformed
240            this.mathMLText = null;
241            this.setVisible(false);
242        }// GEN-LAST:event_cancelButtonActionPerformed
243    
244        public String getMathMLText() {
245            return this.mathMLText;
246        }
247    
248        private void okButtonActionPerformed(final java.awt.event.ActionEvent evt) {// GEN-FIRST:event_okButtonActionPerformed
249    
250            this.updateMathMLText();
251            this.setVisible(false);
252        }// GEN-LAST:event_okButtonActionPerformed
253    
254        private String buildText(final int lines, final int columns,
255                final String type) {
256            // String s = "<" + type + ">" + Helper.nl();
257            String s = "<mrow>" + Helper.nl() + "<mfenced" + type + ">"
258                    + Helper.nl() + "<mtable>" + Helper.nl();
259    
260            for (int i = 0; i < lines; ++i) {
261                // s += "<matrixrow>" + Helper.nl();
262                s += "<mtr>" + Helper.nl();
263                for (int j = 0; j < columns; ++j) {
264                    s += "<mtd><mn>1</mn></mtd>" + Helper.nl();
265                    // s+="\t<cn>1</cn>" + Helper.nl();
266                }
267                // s+= "</matrixrow>" + Helper.nl();
268                s += "</mtr>" + Helper.nl();
269            }
270            // s+="</" + type + ">" + Helper.nl();
271            s += "</mtable>" + Helper.nl() + "</mfenced>" + Helper.nl() + "</mrow>"
272                    + Helper.nl();
273            return s;
274        }
275    
276        private void updateMathMLText() {
277            final int m = (Integer) this.rowsSpinner.getValue();
278            final int n = (Integer) this.columnsSpinner.getValue();
279    
280            // matrix
281            if (this.matrixRadioButton.isSelected()) {
282                this.mathMLText = this.buildText(m, n, "");
283            } else if (this.determinantRadioButton.isSelected()) {
284                this.mathMLText = this.buildText(m, n, " open=\"|\" close=\"|\"");
285            } else if (this.tableRadioButton.isSelected()) {
286                this.mathMLText = this.buildText(m, n, " open=\"\" close=\"\"");
287            } else {
288                this.mathMLText = null;
289            }
290        }
291    
292        /**
293         * @param args
294         *            the command line arguments
295         */
296        public static void main(final String args[]) {
297            java.awt.EventQueue.invokeLater(new Runnable() {
298                public void run() {
299                    final InsertTableDialog dialog = new InsertTableDialog(
300                            new javax.swing.JFrame(), true);
301                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
302                        @Override
303                        public void windowClosing(final java.awt.event.WindowEvent e) {
304                            System.exit(0);
305                        }
306                    });
307                    dialog.setVisible(true);
308                }
309            });
310        }
311    
312        // Variables declaration - do not modify//GEN-BEGIN:variables
313        private javax.swing.ButtonGroup buttonGroupType;
314        private javax.swing.JButton cancelButton;
315        private javax.swing.JLabel columnsLabel;
316        private javax.swing.JSpinner columnsSpinner;
317        private javax.swing.JRadioButton determinantRadioButton;
318        private javax.swing.JRadioButton matrixRadioButton;
319        private javax.swing.JButton okButton;
320        private javax.swing.JLabel rowsLabel;
321        private javax.swing.JSpinner rowsSpinner;
322        private javax.swing.JRadioButton tableRadioButton;
323        private javax.swing.JLabel typeLabel;
324        // End of variables declaration//GEN-END:variables
325    
326    }