~cezary-bartosiak/gephi/spread-simulator

« back to all changes in this revision

Viewing changes to ComplexGeneratorPluginUI/src/org/gephi/ui/complexgenerator/plugin/KleinbergPanel.java

  • Committer: Cezary Bartosiak
  • Date: 2011-02-21 07:47:23 UTC
  • mfrom: (2044.4.7 generators)
  • Revision ID: cezary.bartosiak@gmail.com-20110221074723-ul2u6nkg1evjvh0a
* merged with generators

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2008-2010 Gephi
 
3
 * Authors : Cezary Bartosiak
 
4
 * Website : http://www.gephi.org
 
5
 *
 
6
 * This file is part of Gephi.
 
7
 *
 
8
 * Gephi is free software: you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation, either version 3 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * Gephi is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with Gephi.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
package org.gephi.ui.complexgenerator.plugin;
 
22
 
 
23
import org.netbeans.validation.api.Problems;
 
24
import org.netbeans.validation.api.Validator;
 
25
import org.netbeans.validation.api.builtin.Validators;
 
26
import org.netbeans.validation.api.ui.ValidationGroup;
 
27
import org.netbeans.validation.api.ui.ValidationPanel;
 
28
 
 
29
/**
 
30
 *
 
31
 *
 
32
 * @author Cezary Bartosiak
 
33
 */
 
34
public class KleinbergPanel extends javax.swing.JPanel {
 
35
 
 
36
    /** Creates new form BarabasiAlbertPanel */
 
37
    public KleinbergPanel() {
 
38
        initComponents();
 
39
    }
 
40
 
 
41
        public static ValidationPanel createValidationPanel(KleinbergPanel innerPanel) {
 
42
                ValidationPanel validationPanel = new ValidationPanel();
 
43
                if (innerPanel == null)
 
44
                        innerPanel = new KleinbergPanel();
 
45
                validationPanel.setInnerComponent(innerPanel);
 
46
 
 
47
                ValidationGroup group = validationPanel.getValidationGroup();
 
48
 
 
49
                group.add(innerPanel.nField, Validators.REQUIRE_NON_EMPTY_STRING,
 
50
                                new nValidator(innerPanel));
 
51
                group.add(innerPanel.pField, Validators.REQUIRE_NON_EMPTY_STRING,
 
52
                                new pValidator(innerPanel));
 
53
                group.add(innerPanel.qField, Validators.REQUIRE_NON_EMPTY_STRING,
 
54
                                new qValidator(innerPanel));
 
55
                group.add(innerPanel.rField, Validators.REQUIRE_NON_EMPTY_STRING,
 
56
                                new rValidator(innerPanel));
 
57
 
 
58
                return validationPanel;
 
59
        }
 
60
 
 
61
        private static class nValidator implements Validator<String> {
 
62
                private KleinbergPanel innerPanel;
 
63
 
 
64
                public nValidator(KleinbergPanel innerPanel) {
 
65
                        this.innerPanel = innerPanel;
 
66
                }
 
67
 
 
68
                @Override
 
69
                public boolean validate(Problems problems, String compName, String model) {
 
70
                        boolean result = false;
 
71
 
 
72
                        try {
 
73
                                Integer n = Integer.parseInt(innerPanel.nField.getText());
 
74
                                result = n >= 2;
 
75
                        }
 
76
                        catch (Exception e) { }
 
77
                        if (!result) {
 
78
                                String message = "<html>n &gt;= 2</html>";
 
79
                                problems.add(message);
 
80
                        }
 
81
 
 
82
                        return result;
 
83
                }
 
84
    }
 
85
 
 
86
        private static class pValidator implements Validator<String> {
 
87
                private KleinbergPanel innerPanel;
 
88
 
 
89
                public pValidator(KleinbergPanel innerPanel) {
 
90
                        this.innerPanel = innerPanel;
 
91
                }
 
92
 
 
93
                @Override
 
94
                public boolean validate(Problems problems, String compName, String model) {
 
95
                        boolean result = false;
 
96
 
 
97
                        try {
 
98
                                Integer n = Integer.parseInt(innerPanel.nField.getText());
 
99
                                Integer p = Integer.parseInt(innerPanel.pField.getText());
 
100
                                result = p >= 1 && p <= 2 * n - 2;
 
101
                        }
 
102
                        catch (Exception e) { }
 
103
                        if (!result) {
 
104
                                String message = "<html>1 &lt;= p &lt;= 2n - 2</html>";
 
105
                                problems.add(message);
 
106
                        }
 
107
 
 
108
                        return result;
 
109
                }
 
110
    }
 
111
 
 
112
        private static class qValidator implements Validator<String> {
 
113
                private KleinbergPanel innerPanel;
 
114
 
 
115
                public qValidator(KleinbergPanel innerPanel) {
 
116
                        this.innerPanel = innerPanel;
 
117
                }
 
118
 
 
119
                @Override
 
120
                public boolean validate(Problems problems, String compName, String model) {
 
121
                        boolean result = false;
 
122
 
 
123
                        try {
 
124
                                Integer n = Integer.parseInt(innerPanel.nField.getText());
 
125
                                Integer p = Integer.parseInt(innerPanel.pField.getText());
 
126
                                Integer q = Integer.parseInt(innerPanel.qField.getText());
 
127
                                if (p < n)
 
128
                                        result = q >= 0 && q <= n * n - p * (p + 3) / 2 - 1;
 
129
                                else result = q >= 0 && q <= (2 * n - p - 3) * (2 * n - p) / 2 + 1;
 
130
                        }
 
131
                        catch (Exception e) { }
 
132
                        if (!result) {
 
133
                                String message =
 
134
                                        "<html>q &gt;= 0<br>" +
 
135
                                        "q &lt;= n^2 - p * (p + 3) / 2 - 1 for p &lt; n<br>" +
 
136
                                        "q &lt;= (2n - p - 3) * (2n - p) / 2 + 1 for p &gt;= n<br></html>";
 
137
                                problems.add(message);
 
138
                        }
 
139
 
 
140
                        return result;
 
141
                }
 
142
    }
 
143
 
 
144
        private static class rValidator implements Validator<String> {
 
145
                private KleinbergPanel innerPanel;
 
146
 
 
147
                public rValidator(KleinbergPanel innerPanel) {
 
148
                        this.innerPanel = innerPanel;
 
149
                }
 
150
 
 
151
                @Override
 
152
                public boolean validate(Problems problems, String compName, String model) {
 
153
                        boolean result = false;
 
154
 
 
155
                        try {
 
156
                                Integer r = Integer.parseInt(innerPanel.rField.getText());
 
157
                                result = r >= 0;
 
158
                        }
 
159
                        catch (Exception e) { }
 
160
                        if (!result) {
 
161
                                String message = "<html>r &gt;= 0</html>";
 
162
                                problems.add(message);
 
163
                        }
 
164
 
 
165
                        return result;
 
166
                }
 
167
    }
 
168
 
 
169
    /** This method is called from within the constructor to
 
170
     * initialize the form.
 
171
     * WARNING: Do NOT modify this code. The content of this method is
 
172
     * always regenerated by the Form Editor.
 
173
     */
 
174
    @SuppressWarnings("unchecked")
 
175
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 
176
    private void initComponents() {
 
177
 
 
178
        qLabel = new javax.swing.JLabel();
 
179
        qField = new javax.swing.JTextField();
 
180
        nField = new javax.swing.JTextField();
 
181
        pField = new javax.swing.JTextField();
 
182
        nLabel = new javax.swing.JLabel();
 
183
        pLabel = new javax.swing.JLabel();
 
184
        constraintsLabel = new javax.swing.JLabel();
 
185
        rLabel = new javax.swing.JLabel();
 
186
        rField = new javax.swing.JTextField();
 
187
        torusCheckBox = new javax.swing.JCheckBox();
 
188
 
 
189
        setPreferredSize(new java.awt.Dimension(399, 247));
 
190
 
 
191
        qLabel.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.qLabel.text")); // NOI18N
 
192
 
 
193
        qField.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.qField.text")); // NOI18N
 
194
 
 
195
        nField.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.nField.text")); // NOI18N
 
196
 
 
197
        pField.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.pField.text")); // NOI18N
 
198
 
 
199
        nLabel.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.nLabel.text")); // NOI18N
 
200
 
 
201
        pLabel.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.pLabel.text")); // NOI18N
 
202
 
 
203
        constraintsLabel.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.constraintsLabel.text")); // NOI18N
 
204
 
 
205
        rLabel.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.rLabel.text")); // NOI18N
 
206
 
 
207
        rField.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.rField.text")); // NOI18N
 
208
 
 
209
        torusCheckBox.setText(org.openide.util.NbBundle.getMessage(KleinbergPanel.class, "KleinbergPanel.torusCheckBox.text")); // NOI18N
 
210
 
 
211
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
 
212
        this.setLayout(layout);
 
213
        layout.setHorizontalGroup(
 
214
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
215
            .addGroup(layout.createSequentialGroup()
 
216
                .addContainerGap()
 
217
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
218
                    .addGroup(layout.createSequentialGroup()
 
219
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
220
                            .addComponent(nLabel)
 
221
                            .addComponent(pLabel)
 
222
                            .addComponent(qLabel)
 
223
                            .addComponent(rLabel))
 
224
                        .addGap(74, 74, 74)
 
225
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
 
226
                            .addComponent(rField, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
 
227
                            .addComponent(pField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
 
228
                            .addComponent(qField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
 
229
                            .addComponent(nField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
 
230
                        .addContainerGap())
 
231
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
 
232
                        .addComponent(constraintsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
233
                        .addGap(82, 82, 82))
 
234
                    .addGroup(layout.createSequentialGroup()
 
235
                        .addComponent(torusCheckBox)
 
236
                        .addContainerGap(306, Short.MAX_VALUE))))
 
237
        );
 
238
        layout.setVerticalGroup(
 
239
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
240
            .addGroup(layout.createSequentialGroup()
 
241
                .addContainerGap()
 
242
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
243
                    .addComponent(nField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
244
                    .addComponent(nLabel))
 
245
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
246
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
247
                    .addComponent(pField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
248
                    .addComponent(pLabel))
 
249
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
250
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
251
                    .addComponent(qField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
252
                    .addComponent(qLabel))
 
253
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
254
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
255
                    .addComponent(rField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
256
                    .addComponent(rLabel))
 
257
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
258
                .addComponent(torusCheckBox)
 
259
                .addGap(4, 4, 4)
 
260
                .addComponent(constraintsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
261
                .addContainerGap())
 
262
        );
 
263
    }// </editor-fold>//GEN-END:initComponents
 
264
 
 
265
 
 
266
    // Variables declaration - do not modify//GEN-BEGIN:variables
 
267
    private javax.swing.JLabel constraintsLabel;
 
268
    protected javax.swing.JTextField nField;
 
269
    private javax.swing.JLabel nLabel;
 
270
    protected javax.swing.JTextField pField;
 
271
    private javax.swing.JLabel pLabel;
 
272
    protected javax.swing.JTextField qField;
 
273
    private javax.swing.JLabel qLabel;
 
274
    protected javax.swing.JTextField rField;
 
275
    private javax.swing.JLabel rLabel;
 
276
    protected javax.swing.JCheckBox torusCheckBox;
 
277
    // End of variables declaration//GEN-END:variables
 
278
 
 
279
}