001 package com.croftsoft.apps.agoracast.c2p;
002
003 import java.awt.*;
004 import java.awt.event.*;
005 import java.io.*;
006 import java.util.*;
007 import javax.swing.*;
008
009 import com.croftsoft.core.gui.ButtonPanel2;
010 import com.croftsoft.core.gui.CheckBoxPanel;
011 import com.croftsoft.core.lang.NullArgumentException;
012 import com.croftsoft.core.lang.Pair;
013 import com.croftsoft.core.lang.StringLib;
014 import com.croftsoft.core.util.log.Log;
015 import com.croftsoft.core.util.pubsub.Subscriber;
016
017 /*********************************************************************
018 * Allows the user to select/deselect fields from a list.
019 *
020 * <p />
021 *
022 * @version
023 * 2002-01-29
024 * @since
025 * 2001-10-11
026 * @author
027 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
028 *********************************************************************/
029
030 public final class AgoracastFieldsPanel
031 extends JPanel
032 implements ActionListener
033 //////////////////////////////////////////////////////////////////////
034 //////////////////////////////////////////////////////////////////////
035 {
036
037 public static final Object DONE_BUTTON_EVENT = new Object ( );
038
039 //
040
041 private final AgoracastMediator agoracastMediator;
042
043 private final Subscriber eventSubscriber;
044
045 private final CheckBoxPanel checkBoxPanel;
046
047 private final JButton doneButton;
048
049 //////////////////////////////////////////////////////////////////////
050 //////////////////////////////////////////////////////////////////////
051
052 public AgoracastFieldsPanel (
053 AgoracastMediator agoracastMediator,
054 Subscriber eventSubscriber )
055 //////////////////////////////////////////////////////////////////////
056 {
057 super ( new BorderLayout ( ), true ); // isDoubleBuffered
058
059 NullArgumentException.check (
060 this.agoracastMediator = agoracastMediator );
061
062 NullArgumentException.check (
063 this.eventSubscriber = eventSubscriber );
064
065 AgoracastLib.setColor ( this, agoracastMediator );
066
067 AgoracastField [ ] agoracastFields
068 = agoracastMediator.getAgoracastFields ( );
069
070 String [ ] fieldNames = new String [ agoracastFields.length ];
071
072 String [ ] descriptors = new String [ agoracastFields.length ];
073
074 for ( int i = 0; i < agoracastFields.length; i++ )
075 {
076 fieldNames [ i ] = agoracastFields [ i ].getName ( );
077
078 descriptors [ i ] = agoracastFields [ i ].getSemantic ( );
079 }
080
081 checkBoxPanel = new CheckBoxPanel ( fieldNames, descriptors,
082 agoracastMediator.getPanelBackgroundColor ( ) );
083
084 add ( new JScrollPane ( checkBoxPanel ), BorderLayout.CENTER );
085
086 doneButton = new JButton ( "Done" );
087
088 doneButton.addActionListener ( this );
089
090 add ( doneButton, BorderLayout.SOUTH );
091 }
092
093 //////////////////////////////////////////////////////////////////////
094 //////////////////////////////////////////////////////////////////////
095
096 public synchronized String [ ] getFieldNames ( )
097 //////////////////////////////////////////////////////////////////////
098 {
099 return checkBoxPanel.getLabelNames ( );
100 }
101
102 public synchronized String [ ] getSelectedFieldNames ( )
103 //////////////////////////////////////////////////////////////////////
104 {
105 return checkBoxPanel.getSelectedLabelNames ( );
106 }
107
108 public synchronized boolean isSelected ( String fieldName )
109 //////////////////////////////////////////////////////////////////////
110 {
111 return checkBoxPanel.isSelected ( fieldName );
112 }
113
114 public synchronized void setSelected (
115 String fieldName,
116 boolean selected )
117 //////////////////////////////////////////////////////////////////////
118 {
119 checkBoxPanel.setSelected ( fieldName, selected );
120 }
121
122 public synchronized void setSelectedFieldNames (
123 String [ ] selectedFieldNames )
124 //////////////////////////////////////////////////////////////////////
125 {
126 checkBoxPanel.setAllSelected ( false );
127
128 for ( int i = 0; i < selectedFieldNames.length; i++ )
129 {
130 checkBoxPanel.setSelected ( selectedFieldNames [ i ], true );
131 }
132 }
133
134 //////////////////////////////////////////////////////////////////////
135 //////////////////////////////////////////////////////////////////////
136
137 public synchronized void actionPerformed ( ActionEvent actionEvent )
138 //////////////////////////////////////////////////////////////////////
139 {
140 try
141 {
142 Object source = actionEvent.getSource ( );
143
144 if ( source == doneButton )
145 {
146 eventSubscriber.receive ( DONE_BUTTON_EVENT );
147 }
148 }
149 catch ( Exception ex )
150 {
151 agoracastMediator.getLog ( ).record ( ex );
152 }
153 }
154
155 //////////////////////////////////////////////////////////////////////
156 //////////////////////////////////////////////////////////////////////
157 }