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