Subversion Repository Public Repository

litesoft

Diff Revisions 948 vs 950 for /trunk/Java/KeyHole/src/org/litesoft/aokeyhole/swing/AttributeTypeSelectorJPanel.java

Diff revisions: vs.
  @@ -1,208 +1,208 @@
1 - // This Source Code is in the Public Domain per: http://unlicense.org
2 - package org.litesoft.aokeyhole.swing;
3 -
4 - import org.litesoft.aokeyhole.objects.*;
5 - import org.litesoft.aokeyhole.objects.attributes.*;
6 - import org.litesoft.aokeyhole.swing.simplecomponents.*;
7 -
8 - import javax.swing.*;
9 - import javax.swing.event.*;
10 - import java.awt.*;
11 - import java.util.*;
12 -
13 - public class AttributeTypeSelectorJPanel extends JPanelPlus implements ChangeListener {
14 - private TopBottom mDerived, mRegular, mVirtual;
15 - private CardedJPanel mCards = new CardedJPanel();
16 -
17 - public AttributeTypeSelectorJPanel( ObjectSet pObjectSet, AttributeSet[] pAttributeSets ) {
18 - super( new BorderLayout() );
19 -
20 - boolean zDerivedOK = pObjectSet.acceptableAttributeType( A_Derived.TYPE );
21 -
22 - Options zDerivedOptions = createDerivedOptions( pObjectSet, pAttributeSets );
23 - Options zRegularOptions = createRegularOptions( pObjectSet, pAttributeSets );
24 - Options zVirtualOptions = createVirtualOptions( pObjectSet, pAttributeSets );
25 -
26 - boolean zSelectDerived = zDerivedOK && !zDerivedOptions.isEmpty();
27 - boolean zSelectRegular = !zSelectDerived && !zRegularOptions.isEmpty();
28 - boolean zSelectVirtual = !zSelectDerived && !zSelectRegular;
29 -
30 - mDerived = addCard( new TopBottom( this, "Derived", zSelectDerived, zDerivedOptions ) );
31 - mRegular = addCard( new TopBottom( this, pObjectSet.labelForRegularAttributes(), zSelectRegular, zRegularOptions ) );
32 - mVirtual = addCard( new TopBottom( this, "Virtual", zSelectVirtual, zVirtualOptions ) );
33 -
34 - JComponent zTopOptions = createTopOptions( mDerived, mVirtual, mRegular ); // Order for GUI
35 - if ( zTopOptions != null ) {
36 - add( zTopOptions, BorderLayout.NORTH );
37 - }
38 - add( mCards, BorderLayout.CENTER );
39 -
40 - showAppropriateCard();
41 - }
42 -
43 - private JComponent createTopOptions( TopBottom... pTBs ) {
44 - ArrayList<JRadioButton> zOptions = new ArrayList<JRadioButton>();
45 - ButtonGroup group = new ButtonGroup();
46 - for ( TopBottom zTB : pTBs ) {
47 - if ( !zTB.getOptions().isEmpty() ) {
48 - JRadioButton zButton = zTB.getTop();
49 - group.add( zButton );
50 - zOptions.add( zButton );
51 - }
52 - }
53 - if ( zOptions.size() < 2 ) {
54 - return null;
55 - }
56 - JPanel rv = new JPanel( new GridLayout( 2, zOptions.size() ) );
57 - // Populate Left (Spacers)
58 - //noinspection ForLoopReplaceableByForEach
59 - for ( int i = 0; i < zOptions.size(); i++ ) {
60 - rv.add( new JLabel( " " ) );
61 - }
62 - // Populate Right (Options)
63 - for ( JRadioButton zOption : zOptions ) {
64 - rv.add( zOption );
65 - }
66 - return rv;
67 - }
68 -
69 - @Override
70 - public void stateChanged( ChangeEvent e ) {
71 - showAppropriateCard();
72 - }
73 -
74 - private boolean showAppropriateCard() {
75 - // Left To Right
76 - return showCard( mDerived ) || showCard( mVirtual ) || showCard( mRegular );
77 - }
78 -
79 - private boolean showCard( TopBottom pTB ) {
80 - if ( pTB.getTop().isSelected() ) {
81 - mCards.showOption( pTB.getTop().getText() );
82 - return true;
83 - }
84 - return false;
85 - }
86 -
87 - private TopBottom addCard( TopBottom pTB ) {
88 - mCards.addOption( centered( pTB ), pTB.getTop().getText() );
89 - return pTB;
90 - }
91 -
92 - private Options createDerivedOptions( ObjectSet pObjectSet, AttributeSet[] pAttributeSets ) {
93 - Options zOptions = new Options();
94 - for ( AttributeSet zSet : pAttributeSets ) {
95 - if ( A_Derived.TYPE.equals( zSet.getType() ) ) {
96 - zOptions.add( pObjectSet, zSet );
97 - }
98 - }
99 - return zOptions;
100 - }
101 -
102 - private Options createVirtualOptions( ObjectSet pObjectSet, AttributeSet[] pAttributeSets ) {
103 - Options zOptions = new Options();
104 - for ( AttributeSet zSet : pAttributeSets ) {
105 - if ( !A_Derived.TYPE.equals( zSet.getType() ) && (zSet.getVirtual() != null) ) {
106 - zOptions.add( pObjectSet, zSet );
107 - }
108 - }
109 - return zOptions;
110 - }
111 -
112 - private Options createRegularOptions( ObjectSet pObjectSet, AttributeSet[] pAttributeSets ) {
113 - Options zOptions = new Options();
114 - for ( AttributeSet zSet : pAttributeSets ) {
115 - if ( !A_Derived.TYPE.equals( zSet.getType() ) && (zSet.getPersisted() != null) ) {
116 - zOptions.add( pObjectSet, zSet );
117 - }
118 - }
119 - return zOptions;
120 - }
121 -
122 - public boolean isVirtual() {
123 - return mVirtual.isSelected();
124 - }
125 -
126 - public AttributeSet determineSelectedAttributeSet() {
127 - if ( mDerived.isSelected() ) {
128 - return determineSelectedAttributeSet( mDerived );
129 - }
130 - if ( mRegular.isSelected() ) {
131 - return determineSelectedAttributeSet( mRegular );
132 - }
133 - return determineSelectedAttributeSet( mVirtual );
134 - }
135 -
136 - private AttributeSet determineSelectedAttributeSet( TopBottom pTB ) {
137 - Options zOptions = pTB.getOptions();
138 - for ( int i = zOptions.size(); --i > 0; ) {
139 - OurRadioButton zButton = zOptions.get( i );
140 - if ( zButton.isSelected() ) {
141 - return zButton.getAttributeSet();
142 - }
143 - }
144 - return zOptions.get( 0 ).getAttributeSet();
145 - }
146 -
147 - private static class OurRadioButton extends JRadioButton {
148 - private AttributeSet mAttributeSet;
149 -
150 - public OurRadioButton( AttributeSet pAttributeSet, boolean selected ) {
151 - super( pAttributeSet.getType().toString(), selected );
152 - mAttributeSet = pAttributeSet;
153 - }
154 -
155 - public AttributeSet getAttributeSet() {
156 - return mAttributeSet;
157 - }
158 - }
159 -
160 - private static class Options extends ArrayList<OurRadioButton> {
161 - private boolean mSelected = true;
162 -
163 - public void add( ObjectSet pObjectSet, AttributeSet pAttributeSet ) {
164 - if ( (pAttributeSet instanceof UiCRUDpSet) && pObjectSet.acceptableAttributeType( pAttributeSet.getType() ) ) {
165 - super.add( new OurRadioButton( pAttributeSet, mSelected ) );
166 - mSelected = false;
167 - }
168 - }
169 - }
170 -
171 - private static class TopBottom extends JPanel {
172 - private JRadioButton mTop;
173 - private Options mOptions = new Options();
174 -
175 - private TopBottom( ChangeListener pChangeListener, String pTopText, boolean pTopSelected, Options pOptions ) {
176 - super( new BorderLayout() );
177 - (mTop = new JRadioButton( pTopText, pTopSelected )).addChangeListener( pChangeListener );
178 - mOptions = pOptions;
179 - add( new JLabel( " " ), BorderLayout.NORTH );
180 - add( centered( createGUI( pOptions ) ), BorderLayout.CENTER );
181 - }
182 -
183 - public JRadioButton getTop() {
184 - return mTop;
185 - }
186 -
187 - public Options getOptions() {
188 - return mOptions;
189 - }
190 -
191 - private static JComponent createGUI( Options pOptions ) {
192 - if ( pOptions.size() < 2 ) {
193 - return new JLabel( " " );
194 - }
195 - ButtonGroup group = new ButtonGroup();
196 - JPanel rv = new JPanel( new GridLayout( pOptions.size(), 1 ) );
197 - for ( OurRadioButton zButton : pOptions ) {
198 - rv.add( zButton );
199 - group.add( zButton );
200 - }
201 - return rv;
202 - }
203 -
204 - public boolean isSelected() {
205 - return mTop.isSelected();
206 - }
207 - }
208 - }
1 + // This Source Code is in the Public Domain per: http://unlicense.org
2 + package org.litesoft.aokeyhole.swing;
3 +
4 + import org.litesoft.aokeyhole.objects.*;
5 + import org.litesoft.aokeyhole.objects.attributes.*;
6 + import org.litesoft.aokeyhole.swing.simplecomponents.*;
7 +
8 + import javax.swing.*;
9 + import javax.swing.event.*;
10 + import java.awt.*;
11 + import java.util.*;
12 +
13 + public class AttributeTypeSelectorJPanel extends JPanelPlus implements ChangeListener {
14 + private TopBottom mDerived, mRegular, mVirtual;
15 + private CardedJPanel mCards = new CardedJPanel();
16 +
17 + public AttributeTypeSelectorJPanel( ObjectSet pObjectSet, AttributeSet[] pAttributeSets ) {
18 + super( new BorderLayout() );
19 +
20 + boolean zDerivedOK = pObjectSet.acceptableAttributeType( A_Derived.TYPE );
21 +
22 + Options zDerivedOptions = createDerivedOptions( pObjectSet, pAttributeSets );
23 + Options zRegularOptions = createRegularOptions( pObjectSet, pAttributeSets );
24 + Options zVirtualOptions = createVirtualOptions( pObjectSet, pAttributeSets );
25 +
26 + boolean zSelectDerived = zDerivedOK && !zDerivedOptions.isEmpty();
27 + boolean zSelectRegular = !zSelectDerived && !zRegularOptions.isEmpty();
28 + boolean zSelectVirtual = !zSelectDerived && !zSelectRegular;
29 +
30 + mDerived = addCard( new TopBottom( this, "Derived", zSelectDerived, zDerivedOptions ) );
31 + mRegular = addCard( new TopBottom( this, pObjectSet.labelForRegularAttributes(), zSelectRegular, zRegularOptions ) );
32 + mVirtual = addCard( new TopBottom( this, "Virtual", zSelectVirtual, zVirtualOptions ) );
33 +
34 + JComponent zTopOptions = createTopOptions( mDerived, mVirtual, mRegular ); // Order for GUI
35 + if ( zTopOptions != null ) {
36 + add( zTopOptions, BorderLayout.NORTH );
37 + }
38 + add( mCards, BorderLayout.CENTER );
39 +
40 + showAppropriateCard();
41 + }
42 +
43 + private JComponent createTopOptions( TopBottom... pTBs ) {
44 + ArrayList<JRadioButton> zOptions = new ArrayList<JRadioButton>();
45 + ButtonGroup group = new ButtonGroup();
46 + for ( TopBottom zTB : pTBs ) {
47 + if ( !zTB.getOptions().isEmpty() ) {
48 + JRadioButton zButton = zTB.getTop();
49 + group.add( zButton );
50 + zOptions.add( zButton );
51 + }
52 + }
53 + if ( zOptions.size() < 2 ) {
54 + return null;
55 + }
56 + JPanel rv = new JPanel( new GridLayout( 2, zOptions.size() ) );
57 + // Populate Left (Spacers)
58 + //noinspection ForLoopReplaceableByForEach
59 + for ( int i = 0; i < zOptions.size(); i++ ) {
60 + rv.add( new JLabel( " " ) );
61 + }
62 + // Populate Right (Options)
63 + for ( JRadioButton zOption : zOptions ) {
64 + rv.add( zOption );
65 + }
66 + return rv;
67 + }
68 +
69 + @Override
70 + public void stateChanged( ChangeEvent e ) {
71 + showAppropriateCard();
72 + }
73 +
74 + private boolean showAppropriateCard() {
75 + // Left To Right
76 + return showCard( mDerived ) || showCard( mVirtual ) || showCard( mRegular );
77 + }
78 +
79 + private boolean showCard( TopBottom pTB ) {
80 + if ( pTB.getTop().isSelected() ) {
81 + mCards.showOption( pTB.getTop().getText() );
82 + return true;
83 + }
84 + return false;
85 + }
86 +
87 + private TopBottom addCard( TopBottom pTB ) {
88 + mCards.addOption( centered( pTB ), pTB.getTop().getText() );
89 + return pTB;
90 + }
91 +
92 + private Options createDerivedOptions( ObjectSet pObjectSet, AttributeSet[] pAttributeSets ) {
93 + Options zOptions = new Options();
94 + for ( AttributeSet zSet : pAttributeSets ) {
95 + if ( A_Derived.TYPE.equals( zSet.getType() ) ) {
96 + zOptions.add( pObjectSet, zSet );
97 + }
98 + }
99 + return zOptions;
100 + }
101 +
102 + private Options createVirtualOptions( ObjectSet pObjectSet, AttributeSet[] pAttributeSets ) {
103 + Options zOptions = new Options();
104 + for ( AttributeSet zSet : pAttributeSets ) {
105 + if ( !A_Derived.TYPE.equals( zSet.getType() ) && (zSet.getVirtual() != null) ) {
106 + zOptions.add( pObjectSet, zSet );
107 + }
108 + }
109 + return zOptions;
110 + }
111 +
112 + private Options createRegularOptions( ObjectSet pObjectSet, AttributeSet[] pAttributeSets ) {
113 + Options zOptions = new Options();
114 + for ( AttributeSet zSet : pAttributeSets ) {
115 + if ( !A_Derived.TYPE.equals( zSet.getType() ) && (zSet.getPersisted() != null) ) {
116 + zOptions.add( pObjectSet, zSet );
117 + }
118 + }
119 + return zOptions;
120 + }
121 +
122 + public boolean isVirtual() {
123 + return mVirtual.isSelected();
124 + }
125 +
126 + public AttributeSet determineSelectedAttributeSet() {
127 + if ( mDerived.isSelected() ) {
128 + return determineSelectedAttributeSet( mDerived );
129 + }
130 + if ( mRegular.isSelected() ) {
131 + return determineSelectedAttributeSet( mRegular );
132 + }
133 + return determineSelectedAttributeSet( mVirtual );
134 + }
135 +
136 + private AttributeSet determineSelectedAttributeSet( TopBottom pTB ) {
137 + Options zOptions = pTB.getOptions();
138 + for ( int i = zOptions.size(); --i > 0; ) {
139 + OurRadioButton zButton = zOptions.get( i );
140 + if ( zButton.isSelected() ) {
141 + return zButton.getAttributeSet();
142 + }
143 + }
144 + return zOptions.get( 0 ).getAttributeSet();
145 + }
146 +
147 + private static class OurRadioButton extends JRadioButton {
148 + private AttributeSet mAttributeSet;
149 +
150 + public OurRadioButton( AttributeSet pAttributeSet, boolean selected ) {
151 + super( pAttributeSet.getType().toString(), selected );
152 + mAttributeSet = pAttributeSet;
153 + }
154 +
155 + public AttributeSet getAttributeSet() {
156 + return mAttributeSet;
157 + }
158 + }
159 +
160 + private static class Options extends ArrayList<OurRadioButton> {
161 + private boolean mSelected = true;
162 +
163 + public void add( ObjectSet pObjectSet, AttributeSet pAttributeSet ) {
164 + if ( (pAttributeSet instanceof UiCRUDpSet) && pObjectSet.acceptableAttributeType( pAttributeSet.getType() ) ) {
165 + super.add( new OurRadioButton( pAttributeSet, mSelected ) );
166 + mSelected = false;
167 + }
168 + }
169 + }
170 +
171 + private static class TopBottom extends JPanel {
172 + private JRadioButton mTop;
173 + private Options mOptions = new Options();
174 +
175 + private TopBottom( ChangeListener pChangeListener, String pTopText, boolean pTopSelected, Options pOptions ) {
176 + super( new BorderLayout() );
177 + (mTop = new JRadioButton( pTopText, pTopSelected )).addChangeListener( pChangeListener );
178 + mOptions = pOptions;
179 + add( new JLabel( " " ), BorderLayout.NORTH );
180 + add( centered( createGUI( pOptions ) ), BorderLayout.CENTER );
181 + }
182 +
183 + public JRadioButton getTop() {
184 + return mTop;
185 + }
186 +
187 + public Options getOptions() {
188 + return mOptions;
189 + }
190 +
191 + private static JComponent createGUI( Options pOptions ) {
192 + if ( pOptions.size() < 2 ) {
193 + return new JLabel( " " );
194 + }
195 + ButtonGroup group = new ButtonGroup();
196 + JPanel rv = new JPanel( new GridLayout( pOptions.size(), 1 ) );
197 + for ( OurRadioButton zButton : pOptions ) {
198 + rv.add( zButton );
199 + group.add( zButton );
200 + }
201 + return rv;
202 + }
203 +
204 + public boolean isSelected() {
205 + return mTop.isSelected();
206 + }
207 + }
208 + }