Subversion Repository Public Repository

litesoft

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
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.aokeyhole.swing;

import org.litesoft.aokeyhole.objects.*;
import org.litesoft.aokeyhole.swing.namevaluecomponents.*;
import org.litesoft.aokeyhole.swing.simplecomponents.*;
import org.litesoft.aokeyhole.toolkit.*;
import org.litesoft.commonfoundation.base.*;

import javax.swing.*;
import java.awt.*;
import java.util.*;

import static org.litesoft.aokeyhole.toolkit.ActionsListener.*;

public class CommonPropertyActionJPanel extends JPanelPlus implements ActionsControllableNameValuesComponent {
    public static final String ACTION_BLANK = "Blank";
    public static final String ACTION_EDIT = "Edit";
    public static final String ACTION_NEW = "New";

    private static final String[] EDIT_ACTIONS_WITH_DELETE = new String[]{ACTION_SAVE, ACTION_DELETE, ACTION_REVERT};
    private static final String[] EDIT_ACTIONS_NO_DELETE = new String[]{ACTION_SAVE, ACTION_REVERT};

    private Map<String, NameValueComponent> mNameValueComponents = new LinkedHashMap<String, NameValueComponent>();
    private ActionCardedJPanel mActionCardedPanel = null;

    private ObjectSet mObjectSet = null;
    private boolean mVirtual = false;
    private AttributeSet mAttributeSet = null;

    public CommonPropertyActionJPanel() {
        super( new BorderLayout() );
    }

    public void set( ObjectSet pObjectSet, boolean pVirtual, AttributeSet pAttributeSet ) {
        mObjectSet = pObjectSet;
        mVirtual = pVirtual;
        mAttributeSet = pAttributeSet;
    }

    @Override
    public ObjectSet getObjectSet() {
        return mObjectSet;
    }

    @Override
    public boolean isVirtual() {
        return mVirtual;
    }

    @Override
    public AttributeSet getAttributeSet() {
        return mAttributeSet;
    }

    public void clear() {
        int zCount = getComponentCount();
        for ( int i = zCount; --i >= 0; ) {
            Component zComponent = getComponent( i );
            remove( i );
            if ( zComponent instanceof Disposable ) {
                ((Disposable) zComponent).dispose();
            }
        }
        mNameValueComponents.clear();
    }

    public CommonPropertyActionJPanel build( String pLabel, ActionsListener pActionsListener, boolean pEditable, boolean pAddDeleteAction,
                                             VerticalGrowableIndicatingComponent[] pVerticalGrowableIndicatingComponents ) {
        for ( VerticalGrowableIndicatingComponent vgic : pVerticalGrowableIndicatingComponents ) {
            if ( vgic instanceof NameValueComponent ) {
                NameValueComponent nvc = (NameValueComponent) vgic;
                if ( mNameValueComponents.put( nvc.getName(), nvc ) != null ) {
                    throw new IllegalStateException( "Dup Named Component: " + nvc.getName() );
                }
            }
        }
        VerticalGrowableIndicatingComponent vgic = createPropertyEditViewJPanel( pVerticalGrowableIndicatingComponents );

        add( centered( pLabel ), BorderLayout.NORTH );
        add( (Component) vgic, BorderLayout.CENTER );

        if ( pEditable ) {
            mActionCardedPanel = new ActionCardedJPanel();

            mActionCardedPanel.add( ACTION_BLANK, new JLabel( " " ) );
            mActionCardedPanel.add( ACTION_NEW, new ActionsJPanel( pActionsListener, ACTION_SAVE, ACTION_CANCEL ) );
            mActionCardedPanel.add( ACTION_EDIT, new ActionsJPanel( pActionsListener, pAddDeleteAction ? EDIT_ACTIONS_WITH_DELETE : EDIT_ACTIONS_NO_DELETE ) );

            add( centered( mActionCardedPanel ), BorderLayout.SOUTH );
        }
        return this;
    }

    protected VerticalGrowableIndicatingComponent createPropertyEditViewJPanel( VerticalGrowableIndicatingComponent[] pVGICs ) {
        int growables = 0;
        for ( VerticalGrowableIndicatingComponent vgic : pVGICs ) {
            if ( vgic.growsVertically() ) {
                growables++;
            }
        }
        return collapse( pVGICs, 0, pVGICs.length, growables );
    }

    protected VerticalGrowableIndicatingComponent collapse( VerticalGrowableIndicatingComponent[] pVGICs, int pFromIndex, int pToIndex, int pGrowables ) {
        if ( (pToIndex - pFromIndex) == 1 ) {
            return pVGICs[pFromIndex];
        }
        if ( pGrowables == 0 ) {
            VerticalJPanel_NGs panel = new VerticalJPanel_NGs();
            for ( int i = pFromIndex; i < pToIndex; i++ ) {
                panel.add( (Component) pVGICs[i] );
            }
            return panel;
        }
        VerticalGrowableIndicatingComponent chunk1, chunk2;
        int firstGrowable = findFirstGrowable( pVGICs, pFromIndex, pToIndex );
        if ( firstGrowable == pFromIndex ) {
            chunk1 = pVGICs[pFromIndex];
            chunk2 = collapse( pVGICs, pFromIndex + 1, pToIndex, pGrowables - 1 );
        } else if ( firstGrowable == (pToIndex - 1) ) {
            chunk1 = collapse( pVGICs, pFromIndex, firstGrowable, pGrowables - 1 );
            chunk2 = pVGICs[firstGrowable];
        } else {
            chunk1 = collapse( pVGICs, pFromIndex, ++firstGrowable, 1 );
            chunk2 = collapse( pVGICs, firstGrowable, pToIndex, pGrowables - 1 );
        }
        if ( chunk1.growsVertically() && chunk2.growsVertically() ) {
            return new VerticalJPanel_GandG( chunk1, chunk2 );
        }
        return new VerticalJPanel_NGandG( chunk1, chunk2 );
    }

    private int findFirstGrowable( VerticalGrowableIndicatingComponent[] pVGICs, int pFromIndex, int pToIndex ) {
        for ( int i = pFromIndex; i < pToIndex; i++ ) {
            if ( pVGICs[i].growsVertically() ) {
                return i;
            }
        }
        throw new IllegalStateException( "No Growable Found" );
    }

    @Override
    public void initializeView() {
        clearValues();
        boolean zFocused = false;
        for ( NameValueComponent nameValueComponent : mNameValueComponents.values() ) {
            nameValueComponent.initializeView();
            if ( !zFocused ) {
                zFocused = nameValueComponent.setFocus();
            }
        }
    }

    @Override
    public NameValuesComponent setUpView( ViewSupportable pViewSupportable ) {
        initializeView();
        pViewSupportable.setUpView( this );
        return this;
    }

    /**
     * @return null or Error
     */
    @Override
    public String update( ViewSupportable pViewSupportable ) {
        return pViewSupportable.saveFromView( this );
    }

    @Override
    public void actionEnable( String pAction, boolean pEnable ) {
        if ( mActionCardedPanel != null ) {
            mActionCardedPanel.actionEnable( pAction, pEnable );
        }
    }

    public void showActions( String pActionsID ) {
        if ( mActionCardedPanel != null ) {
            mActionCardedPanel.showOption( pActionsID );
        }
    }

    @Override
    public String[] getNames() {
        String[] keys = mNameValueComponents.keySet().toArray( new String[mNameValueComponents.size()] );
        Arrays.sort( keys );
        return keys;
    }

    @Override
    public void clearValues() {
        for ( NameValueComponent nameValueComponent : mNameValueComponents.values() ) {
            nameValueComponent.clearValue();
        }
    }

    @Override
    public boolean hasComponentFor( String pName ) {
        return (null != mNameValueComponents.get( pName ));
    }

    @Override
    public String getValue( String pName ) {
        NameValueComponent nvc = mNameValueComponents.get( pName );
        return (nvc == null) ? null : nvc.getValue();
    }

    @Override
    public boolean setValue( String pName, String pValue ) {
        NameValueComponent nvc = mNameValueComponents.get( pName );
        return (nvc != null) && nvc.setValue( pValue );
    }

    @Override
    public boolean setOptions( String pName, String[] pOptions ) {
        NameValueComponent nvc = mNameValueComponents.get( pName );
        return (nvc != null) && nvc.setOptions( pOptions );
    }

    @Override
    public void setEnabled( boolean pEnabled ) {
        for ( NameValueComponent nameValueComponent : mNameValueComponents.values() ) {
            nameValueComponent.setComponentEnabled( pEnabled );
        }
    }

    private static class ActionCardedJPanel extends JPanel implements ActionsControllable {
        private CardLayout mCardLayout;
        private ArrayList<ActionsJPanel> mActionPanels = new ArrayList<ActionsJPanel>();

        public ActionCardedJPanel() {
            setLayout( mCardLayout = new CardLayout() );
        }

        @Override
        public void actionEnable( String pAction, boolean pEnable ) {
            for ( ActionsJPanel mActionPanel : mActionPanels ) {
                mActionPanel.actionEnable( pAction, pEnable );
            }
        }

        protected void showOption( String pOptionKey ) {
            mCardLayout.show( this, pOptionKey );
        }

        public void add( String pAction, ActionsJPanel pActionsJPanel ) {
            mActionPanels.add( pActionsJPanel );
            add( pAction, (Component) pActionsJPanel );
        }
    }
}

Commits for litesoft/trunk/Java/KeyHole/src/org/litesoft/aokeyhole/swing/CommonPropertyActionJPanel.java

Diff revisions: vs.
Revision Author Commited Message
950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

17 Diff Diff GeorgeS picture GeorgeS Thu 18 Feb, 2010 23:54:44 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000