litesoft
@ 950
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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.aokeyhole.swing.namevaluecomponents; import org.litesoft.aokeyhole.objects.*; import org.litesoft.aokeyhole.objects.support.*; import org.litesoft.aokeyhole.swing.*; import org.litesoft.aokeyhole.swing.simplecomponents.*; import org.litesoft.aokeyhole.toolkit.*; import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class BackRefComponent extends NamedComponent { private static final String NORTH_ENABLE = "NE"; private static final String NORTH_DISABLE = "ND"; private static final String CENTER_EXISTING = "CE"; private static final String CENTER_NEW = "CN"; private boolean mIsNew = false; private CardedJPanel mCardsNorth = null; private CardedJPanel mCardsCenter = null; private JCheckBox mOptionNew = null; private JCheckBox mOptionExisting = null; private FloodingJComboBox mExisting = null; private JTextField mNewName = null; @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) private ObjectMetaData mObjectMetaData; // todo: make the Existing option more dynamic! public BackRefComponent( ObjectMetaData pObjectMetaData, NameValueChangedByUserListener pNameValueChangedByUserListener ) { super( new NameValueComponentParams( AttribNamesRelatedPersisted.BACK_REF, "Back Reference", Required.YES ).setNameValueChangedByUserListener( pNameValueChangedByUserListener ) ); mObjectMetaData = pObjectMetaData; } @Override public boolean setFocus() { return false; } @Override protected JComponent createNorthContent() { mCardsNorth = new CardedJPanel(); mCardsNorth.addOption( createNewExistingSelector(), NORTH_ENABLE ); mCardsNorth.addOption( new JLabel( " " ), NORTH_DISABLE ); JPanel panel = new JPanel( new BorderLayout( 0, 0 ) ); panel.add( createLabel(), BorderLayout.CENTER ); panel.add( mCardsNorth, BorderLayout.EAST ); return panel; } private static class ShortCheckBox extends JCheckBox { private JLabel mLabel; public ShortCheckBox( String text, boolean selected, JLabel pLabel ) { super( text, selected ); mLabel = pLabel; } @Override public Dimension getPreferredSize() { int prefWidth = super.getPreferredSize().width; int prefHeight = mLabel.getPreferredSize().height; return new Dimension( prefWidth, prefHeight ); } } private JComponent createNewExistingSelector() { JLabel label = new JLabel( "(" ); mOptionNew = new ShortCheckBox( "New", false, label ); mOptionExisting = new ShortCheckBox( "Existing", true, label ); ButtonGroup group = new ButtonGroup(); group.add( mOptionNew ); group.add( mOptionExisting ); mOptionNew.addChangeListener( new ChangeListener() { @Override public void stateChanged( ChangeEvent e ) { newExistingClicked(); } } ); JPanel panel = new JPanel( new FlowLayout( FlowLayout.LEFT, 0, 0 ) ); panel.add( label ); panel.add( mOptionNew ); panel.add( new JLabel( " " ) ); panel.add( mOptionExisting ); panel.add( new JLabel( ")" ) ); return panel; } @Override protected JComponent createCenterContent() { mExisting = new OurComboBox( !isDataRequired() ); mExisting.addActionListener( new JComboBoxChangedListener( this ) ); mNewName = new JTextFieldPlus(); mNewName.getDocument().addDocumentListener( new DocumentChangeListener( this ) ); JPanel innerPanel = new JPanel( new BorderLayout( 0, 0 ) ); innerPanel.add( mNewName, BorderLayout.CENTER ); mCardsCenter = new CardedJPanel(); mCardsCenter.addOption( noGrow( mExisting ), CENTER_EXISTING ); mCardsCenter.addOption( innerPanel, CENTER_NEW ); return mCardsCenter; } @Override protected boolean LLisComponentEnabled() { return mExisting.isEnabled(); } @Override protected void LLsetComponentEnabled( boolean pEnable ) { if ( pEnable ) { mExisting.setEnabled( true ); mCardsNorth.showOption( NORTH_ENABLE ); mCardsCenter.showOption( mIsNew ? CENTER_NEW : CENTER_EXISTING ); } else { mExisting.setEnabled( false ); mCardsNorth.showOption( NORTH_DISABLE ); mCardsCenter.showOption( CENTER_EXISTING ); } } private void newExistingClicked() { if ( mIsNew != mOptionNew.isSelected() ) { Object curValue = getComponentValue(); mIsNew = mOptionNew.isSelected(); mCardsCenter.showOption( mIsNew ? CENTER_NEW : CENTER_EXISTING ); if ( curValue == null ) { rawSetComponentValue( "" ); return; } if ( !"".equals( curValue ) ) { rawSetComponentValue( "" ); valueChanged(); } } } @Override public Object getComponentValue() { if ( mIsNew ) { String newName = AbstractBase.deNull( mNewName.getText() ).trim(); if ( newName.length() == 0 ) { return newName; } return "!" + newName; } Object item = mExisting.getSelectedItem(); return (item != null) ? item.toString() : item; } @Override public void setComponentValue( Object pValue ) { if ( pValue != null ) { String newName = pValue.toString().trim(); if ( newName.startsWith( "!" ) ) { newName = newName.substring( 1 ).trim(); if ( newName.length() != 0 ) { mIsNew = true; mOptionNew.setSelected( mIsNew ); mCardsCenter.showOption( CENTER_NEW ); mNewName.setText( newName ); return; } pValue = ""; } } // Allways an Existing... mIsNew = false; mCardsCenter.showOption( CENTER_EXISTING ); mOptionExisting.setSelected( true ); rawSetComponentValue( pValue ); } public void rawSetComponentValue( Object pValue ) { boolean prevSystemUpdate = mSystemUpdate; mSystemUpdate = true; try { mExisting.setSelectedItem( mExisting.ensureAnOption( pValue ) ); } finally { mSystemUpdate = prevSystemUpdate; } } @Override public boolean setOptions( String[] pOptions ) { mSystemUpdate = true; try { mExisting.setOptions( pOptions, isDataRequired() ); } finally { mSystemUpdate = false; } return true; } private static class OurComboBox extends FloodingJComboBox { public OurComboBox( boolean pAddBlank ) { super( null, pAddBlank ); } @Override public Object ensureAnOption( Object pValue ) { if ( (pValue == null) || "".equals( pValue ) ) { return super.ensureAnOption( pValue ); } String value = pValue.toString() + ":"; int items = getItemCount(); for ( int i = 0; i < items; i++ ) { Object item = getItemAt( i ); if ( item != null ) { if ( item.toString().startsWith( value ) ) { return item; } } } addItem( pValue ); return pValue; } } } |
Commits for litesoft/trunk/Java/KeyHole/src/org/litesoft/aokeyhole/swing/namevaluecomponents/BackRefComponent.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
17 Diff | GeorgeS | Thu 18 Feb, 2010 23:54:44 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |