Subversion Repository Public Repository

litesoft

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

Diff revisions: vs.
  @@ -1,251 +1,251 @@
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.swing.namevaluecomponents.*;
6 - import org.litesoft.aokeyhole.swing.simplecomponents.*;
7 - import org.litesoft.aokeyhole.toolkit.*;
8 - import org.litesoft.commonfoundation.base.*;
9 -
10 - import javax.swing.*;
11 - import java.awt.*;
12 - import java.util.*;
13 -
14 - import static org.litesoft.aokeyhole.toolkit.ActionsListener.*;
15 -
16 - public class CommonPropertyActionJPanel extends JPanelPlus implements ActionsControllableNameValuesComponent {
17 - public static final String ACTION_BLANK = "Blank";
18 - public static final String ACTION_EDIT = "Edit";
19 - public static final String ACTION_NEW = "New";
20 -
21 - private static final String[] EDIT_ACTIONS_WITH_DELETE = new String[]{ACTION_SAVE, ACTION_DELETE, ACTION_REVERT};
22 - private static final String[] EDIT_ACTIONS_NO_DELETE = new String[]{ACTION_SAVE, ACTION_REVERT};
23 -
24 - private Map<String, NameValueComponent> mNameValueComponents = new LinkedHashMap<String, NameValueComponent>();
25 - private ActionCardedJPanel mActionCardedPanel = null;
26 -
27 - private ObjectSet mObjectSet = null;
28 - private boolean mVirtual = false;
29 - private AttributeSet mAttributeSet = null;
30 -
31 - public CommonPropertyActionJPanel() {
32 - super( new BorderLayout() );
33 - }
34 -
35 - public void set( ObjectSet pObjectSet, boolean pVirtual, AttributeSet pAttributeSet ) {
36 - mObjectSet = pObjectSet;
37 - mVirtual = pVirtual;
38 - mAttributeSet = pAttributeSet;
39 - }
40 -
41 - @Override
42 - public ObjectSet getObjectSet() {
43 - return mObjectSet;
44 - }
45 -
46 - @Override
47 - public boolean isVirtual() {
48 - return mVirtual;
49 - }
50 -
51 - @Override
52 - public AttributeSet getAttributeSet() {
53 - return mAttributeSet;
54 - }
55 -
56 - public void clear() {
57 - int zCount = getComponentCount();
58 - for ( int i = zCount; --i >= 0; ) {
59 - Component zComponent = getComponent( i );
60 - remove( i );
61 - if ( zComponent instanceof Disposable ) {
62 - ((Disposable) zComponent).dispose();
63 - }
64 - }
65 - mNameValueComponents.clear();
66 - }
67 -
68 - public CommonPropertyActionJPanel build( String pLabel, ActionsListener pActionsListener, boolean pEditable, boolean pAddDeleteAction,
69 - VerticalGrowableIndicatingComponent[] pVerticalGrowableIndicatingComponents ) {
70 - for ( VerticalGrowableIndicatingComponent vgic : pVerticalGrowableIndicatingComponents ) {
71 - if ( vgic instanceof NameValueComponent ) {
72 - NameValueComponent nvc = (NameValueComponent) vgic;
73 - if ( mNameValueComponents.put( nvc.getName(), nvc ) != null ) {
74 - throw new IllegalStateException( "Dup Named Component: " + nvc.getName() );
75 - }
76 - }
77 - }
78 - VerticalGrowableIndicatingComponent vgic = createPropertyEditViewJPanel( pVerticalGrowableIndicatingComponents );
79 -
80 - add( centered( pLabel ), BorderLayout.NORTH );
81 - add( (Component) vgic, BorderLayout.CENTER );
82 -
83 - if ( pEditable ) {
84 - mActionCardedPanel = new ActionCardedJPanel();
85 -
86 - mActionCardedPanel.add( ACTION_BLANK, new JLabel( " " ) );
87 - mActionCardedPanel.add( ACTION_NEW, new ActionsJPanel( pActionsListener, ACTION_SAVE, ACTION_CANCEL ) );
88 - mActionCardedPanel.add( ACTION_EDIT, new ActionsJPanel( pActionsListener, pAddDeleteAction ? EDIT_ACTIONS_WITH_DELETE : EDIT_ACTIONS_NO_DELETE ) );
89 -
90 - add( centered( mActionCardedPanel ), BorderLayout.SOUTH );
91 - }
92 - return this;
93 - }
94 -
95 - protected VerticalGrowableIndicatingComponent createPropertyEditViewJPanel( VerticalGrowableIndicatingComponent[] pVGICs ) {
96 - int growables = 0;
97 - for ( VerticalGrowableIndicatingComponent vgic : pVGICs ) {
98 - if ( vgic.growsVertically() ) {
99 - growables++;
100 - }
101 - }
102 - return collapse( pVGICs, 0, pVGICs.length, growables );
103 - }
104 -
105 - protected VerticalGrowableIndicatingComponent collapse( VerticalGrowableIndicatingComponent[] pVGICs, int pFromIndex, int pToIndex, int pGrowables ) {
106 - if ( (pToIndex - pFromIndex) == 1 ) {
107 - return pVGICs[pFromIndex];
108 - }
109 - if ( pGrowables == 0 ) {
110 - VerticalJPanel_NGs panel = new VerticalJPanel_NGs();
111 - for ( int i = pFromIndex; i < pToIndex; i++ ) {
112 - panel.add( (Component) pVGICs[i] );
113 - }
114 - return panel;
115 - }
116 - VerticalGrowableIndicatingComponent chunk1, chunk2;
117 - int firstGrowable = findFirstGrowable( pVGICs, pFromIndex, pToIndex );
118 - if ( firstGrowable == pFromIndex ) {
119 - chunk1 = pVGICs[pFromIndex];
120 - chunk2 = collapse( pVGICs, pFromIndex + 1, pToIndex, pGrowables - 1 );
121 - } else if ( firstGrowable == (pToIndex - 1) ) {
122 - chunk1 = collapse( pVGICs, pFromIndex, firstGrowable, pGrowables - 1 );
123 - chunk2 = pVGICs[firstGrowable];
124 - } else {
125 - chunk1 = collapse( pVGICs, pFromIndex, ++firstGrowable, 1 );
126 - chunk2 = collapse( pVGICs, firstGrowable, pToIndex, pGrowables - 1 );
127 - }
128 - if ( chunk1.growsVertically() && chunk2.growsVertically() ) {
129 - return new VerticalJPanel_GandG( chunk1, chunk2 );
130 - }
131 - return new VerticalJPanel_NGandG( chunk1, chunk2 );
132 - }
133 -
134 - private int findFirstGrowable( VerticalGrowableIndicatingComponent[] pVGICs, int pFromIndex, int pToIndex ) {
135 - for ( int i = pFromIndex; i < pToIndex; i++ ) {
136 - if ( pVGICs[i].growsVertically() ) {
137 - return i;
138 - }
139 - }
140 - throw new IllegalStateException( "No Growable Found" );
141 - }
142 -
143 - @Override
144 - public void initializeView() {
145 - clearValues();
146 - boolean zFocused = false;
147 - for ( NameValueComponent nameValueComponent : mNameValueComponents.values() ) {
148 - nameValueComponent.initializeView();
149 - if ( !zFocused ) {
150 - zFocused = nameValueComponent.setFocus();
151 - }
152 - }
153 - }
154 -
155 - @Override
156 - public NameValuesComponent setUpView( ViewSupportable pViewSupportable ) {
157 - initializeView();
158 - pViewSupportable.setUpView( this );
159 - return this;
160 - }
161 -
162 - /**
163 - * @return null or Error
164 - */
165 - @Override
166 - public String update( ViewSupportable pViewSupportable ) {
167 - return pViewSupportable.saveFromView( this );
168 - }
169 -
170 - @Override
171 - public void actionEnable( String pAction, boolean pEnable ) {
172 - if ( mActionCardedPanel != null ) {
173 - mActionCardedPanel.actionEnable( pAction, pEnable );
174 - }
175 - }
176 -
177 - public void showActions( String pActionsID ) {
178 - if ( mActionCardedPanel != null ) {
179 - mActionCardedPanel.showOption( pActionsID );
180 - }
181 - }
182 -
183 - @Override
184 - public String[] getNames() {
185 - String[] keys = mNameValueComponents.keySet().toArray( new String[mNameValueComponents.size()] );
186 - Arrays.sort( keys );
187 - return keys;
188 - }
189 -
190 - @Override
191 - public void clearValues() {
192 - for ( NameValueComponent nameValueComponent : mNameValueComponents.values() ) {
193 - nameValueComponent.clearValue();
194 - }
195 - }
196 -
197 - @Override
198 - public boolean hasComponentFor( String pName ) {
199 - return (null != mNameValueComponents.get( pName ));
200 - }
201 -
202 - @Override
203 - public String getValue( String pName ) {
204 - NameValueComponent nvc = mNameValueComponents.get( pName );
205 - return (nvc == null) ? null : nvc.getValue();
206 - }
207 -
208 - @Override
209 - public boolean setValue( String pName, String pValue ) {
210 - NameValueComponent nvc = mNameValueComponents.get( pName );
211 - return (nvc != null) && nvc.setValue( pValue );
212 - }
213 -
214 - @Override
215 - public boolean setOptions( String pName, String[] pOptions ) {
216 - NameValueComponent nvc = mNameValueComponents.get( pName );
217 - return (nvc != null) && nvc.setOptions( pOptions );
218 - }
219 -
220 - @Override
221 - public void setEnabled( boolean pEnabled ) {
222 - for ( NameValueComponent nameValueComponent : mNameValueComponents.values() ) {
223 - nameValueComponent.setComponentEnabled( pEnabled );
224 - }
225 - }
226 -
227 - private static class ActionCardedJPanel extends JPanel implements ActionsControllable {
228 - private CardLayout mCardLayout;
229 - private ArrayList<ActionsJPanel> mActionPanels = new ArrayList<ActionsJPanel>();
230 -
231 - public ActionCardedJPanel() {
232 - setLayout( mCardLayout = new CardLayout() );
233 - }
234 -
235 - @Override
236 - public void actionEnable( String pAction, boolean pEnable ) {
237 - for ( ActionsJPanel mActionPanel : mActionPanels ) {
238 - mActionPanel.actionEnable( pAction, pEnable );
239 - }
240 - }
241 -
242 - protected void showOption( String pOptionKey ) {
243 - mCardLayout.show( this, pOptionKey );
244 - }
245 -
246 - public void add( String pAction, ActionsJPanel pActionsJPanel ) {
247 - mActionPanels.add( pActionsJPanel );
248 - add( pAction, (Component) pActionsJPanel );
249 - }
250 - }
251 - }
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.swing.namevaluecomponents.*;
6 + import org.litesoft.aokeyhole.swing.simplecomponents.*;
7 + import org.litesoft.aokeyhole.toolkit.*;
8 + import org.litesoft.commonfoundation.base.*;
9 +
10 + import javax.swing.*;
11 + import java.awt.*;
12 + import java.util.*;
13 +
14 + import static org.litesoft.aokeyhole.toolkit.ActionsListener.*;
15 +
16 + public class CommonPropertyActionJPanel extends JPanelPlus implements ActionsControllableNameValuesComponent {
17 + public static final String ACTION_BLANK = "Blank";
18 + public static final String ACTION_EDIT = "Edit";
19 + public static final String ACTION_NEW = "New";
20 +
21 + private static final String[] EDIT_ACTIONS_WITH_DELETE = new String[]{ACTION_SAVE, ACTION_DELETE, ACTION_REVERT};
22 + private static final String[] EDIT_ACTIONS_NO_DELETE = new String[]{ACTION_SAVE, ACTION_REVERT};
23 +
24 + private Map<String, NameValueComponent> mNameValueComponents = new LinkedHashMap<String, NameValueComponent>();
25 + private ActionCardedJPanel mActionCardedPanel = null;
26 +
27 + private ObjectSet mObjectSet = null;
28 + private boolean mVirtual = false;
29 + private AttributeSet mAttributeSet = null;
30 +
31 + public CommonPropertyActionJPanel() {
32 + super( new BorderLayout() );
33 + }
34 +
35 + public void set( ObjectSet pObjectSet, boolean pVirtual, AttributeSet pAttributeSet ) {
36 + mObjectSet = pObjectSet;
37 + mVirtual = pVirtual;
38 + mAttributeSet = pAttributeSet;
39 + }
40 +
41 + @Override
42 + public ObjectSet getObjectSet() {
43 + return mObjectSet;
44 + }
45 +
46 + @Override
47 + public boolean isVirtual() {
48 + return mVirtual;
49 + }
50 +
51 + @Override
52 + public AttributeSet getAttributeSet() {
53 + return mAttributeSet;
54 + }
55 +
56 + public void clear() {
57 + int zCount = getComponentCount();
58 + for ( int i = zCount; --i >= 0; ) {
59 + Component zComponent = getComponent( i );
60 + remove( i );
61 + if ( zComponent instanceof Disposable ) {
62 + ((Disposable) zComponent).dispose();
63 + }
64 + }
65 + mNameValueComponents.clear();
66 + }
67 +
68 + public CommonPropertyActionJPanel build( String pLabel, ActionsListener pActionsListener, boolean pEditable, boolean pAddDeleteAction,
69 + VerticalGrowableIndicatingComponent[] pVerticalGrowableIndicatingComponents ) {
70 + for ( VerticalGrowableIndicatingComponent vgic : pVerticalGrowableIndicatingComponents ) {
71 + if ( vgic instanceof NameValueComponent ) {
72 + NameValueComponent nvc = (NameValueComponent) vgic;
73 + if ( mNameValueComponents.put( nvc.getName(), nvc ) != null ) {
74 + throw new IllegalStateException( "Dup Named Component: " + nvc.getName() );
75 + }
76 + }
77 + }
78 + VerticalGrowableIndicatingComponent vgic = createPropertyEditViewJPanel( pVerticalGrowableIndicatingComponents );
79 +
80 + add( centered( pLabel ), BorderLayout.NORTH );
81 + add( (Component) vgic, BorderLayout.CENTER );
82 +
83 + if ( pEditable ) {
84 + mActionCardedPanel = new ActionCardedJPanel();
85 +
86 + mActionCardedPanel.add( ACTION_BLANK, new JLabel( " " ) );
87 + mActionCardedPanel.add( ACTION_NEW, new ActionsJPanel( pActionsListener, ACTION_SAVE, ACTION_CANCEL ) );
88 + mActionCardedPanel.add( ACTION_EDIT, new ActionsJPanel( pActionsListener, pAddDeleteAction ? EDIT_ACTIONS_WITH_DELETE : EDIT_ACTIONS_NO_DELETE ) );
89 +
90 + add( centered( mActionCardedPanel ), BorderLayout.SOUTH );
91 + }
92 + return this;
93 + }
94 +
95 + protected VerticalGrowableIndicatingComponent createPropertyEditViewJPanel( VerticalGrowableIndicatingComponent[] pVGICs ) {
96 + int growables = 0;
97 + for ( VerticalGrowableIndicatingComponent vgic : pVGICs ) {
98 + if ( vgic.growsVertically() ) {
99 + growables++;
100 + }
101 + }
102 + return collapse( pVGICs, 0, pVGICs.length, growables );
103 + }
104 +
105 + protected VerticalGrowableIndicatingComponent collapse( VerticalGrowableIndicatingComponent[] pVGICs, int pFromIndex, int pToIndex, int pGrowables ) {
106 + if ( (pToIndex - pFromIndex) == 1 ) {
107 + return pVGICs[pFromIndex];
108 + }
109 + if ( pGrowables == 0 ) {
110 + VerticalJPanel_NGs panel = new VerticalJPanel_NGs();
111 + for ( int i = pFromIndex; i < pToIndex; i++ ) {
112 + panel.add( (Component) pVGICs[i] );
113 + }
114 + return panel;
115 + }
116 + VerticalGrowableIndicatingComponent chunk1, chunk2;
117 + int firstGrowable = findFirstGrowable( pVGICs, pFromIndex, pToIndex );
118 + if ( firstGrowable == pFromIndex ) {
119 + chunk1 = pVGICs[pFromIndex];
120 + chunk2 = collapse( pVGICs, pFromIndex + 1, pToIndex, pGrowables - 1 );
121 + } else if ( firstGrowable == (pToIndex - 1) ) {
122 + chunk1 = collapse( pVGICs, pFromIndex, firstGrowable, pGrowables - 1 );
123 + chunk2 = pVGICs[firstGrowable];
124 + } else {
125 + chunk1 = collapse( pVGICs, pFromIndex, ++firstGrowable, 1 );
126 + chunk2 = collapse( pVGICs, firstGrowable, pToIndex, pGrowables - 1 );
127 + }
128 + if ( chunk1.growsVertically() && chunk2.growsVertically() ) {
129 + return new VerticalJPanel_GandG( chunk1, chunk2 );
130 + }
131 + return new VerticalJPanel_NGandG( chunk1, chunk2 );
132 + }
133 +
134 + private int findFirstGrowable( VerticalGrowableIndicatingComponent[] pVGICs, int pFromIndex, int pToIndex ) {
135 + for ( int i = pFromIndex; i < pToIndex; i++ ) {
136 + if ( pVGICs[i].growsVertically() ) {
137 + return i;
138 + }
139 + }
140 + throw new IllegalStateException( "No Growable Found" );
141 + }
142 +
143 + @Override
144 + public void initializeView() {
145 + clearValues();
146 + boolean zFocused = false;
147 + for ( NameValueComponent nameValueComponent : mNameValueComponents.values() ) {
148 + nameValueComponent.initializeView();
149 + if ( !zFocused ) {
150 + zFocused = nameValueComponent.setFocus();
151 + }
152 + }
153 + }
154 +
155 + @Override
156 + public NameValuesComponent setUpView( ViewSupportable pViewSupportable ) {
157 + initializeView();
158 + pViewSupportable.setUpView( this );
159 + return this;
160 + }
161 +
162 + /**
163 + * @return null or Error
164 + */
165 + @Override
166 + public String update( ViewSupportable pViewSupportable ) {
167 + return pViewSupportable.saveFromView( this );
168 + }
169 +
170 + @Override
171 + public void actionEnable( String pAction, boolean pEnable ) {
172 + if ( mActionCardedPanel != null ) {
173 + mActionCardedPanel.actionEnable( pAction, pEnable );
174 + }
175 + }
176 +
177 + public void showActions( String pActionsID ) {
178 + if ( mActionCardedPanel != null ) {
179 + mActionCardedPanel.showOption( pActionsID );
180 + }
181 + }
182 +
183 + @Override
184 + public String[] getNames() {
185 + String[] keys = mNameValueComponents.keySet().toArray( new String[mNameValueComponents.size()] );
186 + Arrays.sort( keys );
187 + return keys;
188 + }
189 +
190 + @Override
191 + public void clearValues() {
192 + for ( NameValueComponent nameValueComponent : mNameValueComponents.values() ) {
193 + nameValueComponent.clearValue();
194 + }
195 + }
196 +
197 + @Override
198 + public boolean hasComponentFor( String pName ) {
199 + return (null != mNameValueComponents.get( pName ));
200 + }
201 +
202 + @Override
203 + public String getValue( String pName ) {
204 + NameValueComponent nvc = mNameValueComponents.get( pName );
205 + return (nvc == null) ? null : nvc.getValue();
206 + }
207 +
208 + @Override
209 + public boolean setValue( String pName, String pValue ) {
210 + NameValueComponent nvc = mNameValueComponents.get( pName );
211 + return (nvc != null) && nvc.setValue( pValue );
212 + }
213 +
214 + @Override
215 + public boolean setOptions( String pName, String[] pOptions ) {
216 + NameValueComponent nvc = mNameValueComponents.get( pName );
217 + return (nvc != null) && nvc.setOptions( pOptions );
218 + }
219 +
220 + @Override
221 + public void setEnabled( boolean pEnabled ) {
222 + for ( NameValueComponent nameValueComponent : mNameValueComponents.values() ) {
223 + nameValueComponent.setComponentEnabled( pEnabled );
224 + }
225 + }
226 +
227 + private static class ActionCardedJPanel extends JPanel implements ActionsControllable {
228 + private CardLayout mCardLayout;
229 + private ArrayList<ActionsJPanel> mActionPanels = new ArrayList<ActionsJPanel>();
230 +
231 + public ActionCardedJPanel() {
232 + setLayout( mCardLayout = new CardLayout() );
233 + }
234 +
235 + @Override
236 + public void actionEnable( String pAction, boolean pEnable ) {
237 + for ( ActionsJPanel mActionPanel : mActionPanels ) {
238 + mActionPanel.actionEnable( pAction, pEnable );
239 + }
240 + }
241 +
242 + protected void showOption( String pOptionKey ) {
243 + mCardLayout.show( this, pOptionKey );
244 + }
245 +
246 + public void add( String pAction, ActionsJPanel pActionsJPanel ) {
247 + mActionPanels.add( pActionsJPanel );
248 + add( pAction, (Component) pActionsJPanel );
249 + }
250 + }
251 + }