001 package com.croftsoft.apps.agoracast.c2p; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import javax.swing.*; 006 import javax.swing.event.*; 007 008 import com.croftsoft.core.gui.ButtonPanel2; 009 import com.croftsoft.core.lang.NullArgumentException; 010 import com.croftsoft.core.lang.ObjectLib; 011 012 /********************************************************************* 013 * @version 014 * 2002-01-29 015 * @since 016 * 2001-09-12 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 *********************************************************************/ 020 021 public final class AgoracastDefaultsPanel 022 extends JPanel 023 implements ActionListener, ChangeListener 024 ////////////////////////////////////////////////////////////////////// 025 ////////////////////////////////////////////////////////////////////// 026 { 027 028 private final AgoracastMediator agoracastMediator; 029 030 private final AgoracastInputPanel agoracastInputPanel; 031 032 private final JButton restoreButton; 033 034 private final JButton updateButton; 035 036 private boolean reactToChanges; 037 038 ////////////////////////////////////////////////////////////////////// 039 ////////////////////////////////////////////////////////////////////// 040 041 public AgoracastDefaultsPanel ( 042 AgoracastMediator agoracastMediator ) 043 ////////////////////////////////////////////////////////////////////// 044 { 045 super ( new BorderLayout ( ), true ); // isDoubleBuffered 046 047 NullArgumentException.check ( 048 this.agoracastMediator = agoracastMediator ); 049 050 AgoracastLib.setColor ( this, agoracastMediator ); 051 052 reactToChanges = true; 053 054 agoracastInputPanel = new AgoracastInputPanel ( agoracastMediator, 055 agoracastMediator.getAgoracastFieldNames ( ), 056 ( ChangeListener ) this ); 057 058 add ( new JScrollPane ( agoracastInputPanel ), 059 BorderLayout.CENTER ); 060 061 restoreButton = new JButton ( "Restore" ); 062 063 restoreButton.setEnabled ( false ); 064 065 restoreButton.addActionListener ( this ); 066 067 updateButton = new JButton ( "Update" ); 068 069 updateButton.setEnabled ( false ); 070 071 updateButton.addActionListener ( this ); 072 073 add ( new ButtonPanel2 ( 074 new JButton [ ] { restoreButton, updateButton } ), 075 BorderLayout.SOUTH ); 076 } 077 078 ////////////////////////////////////////////////////////////////////// 079 ////////////////////////////////////////////////////////////////////// 080 081 public String getValue ( String name ) 082 ////////////////////////////////////////////////////////////////////// 083 { 084 return agoracastInputPanel.getValue ( name ); 085 } 086 087 ////////////////////////////////////////////////////////////////////// 088 ////////////////////////////////////////////////////////////////////// 089 090 public synchronized void stateChanged ( ChangeEvent changeEvent ) 091 ////////////////////////////////////////////////////////////////////// 092 { 093 if ( reactToChanges ) 094 { 095 updateButton.setEnabled ( true ); 096 097 restoreButton.setEnabled ( true ); 098 099 reactToChanges = false; 100 } 101 } 102 103 ////////////////////////////////////////////////////////////////////// 104 ////////////////////////////////////////////////////////////////////// 105 106 public synchronized void actionPerformed ( ActionEvent actionEvent ) 107 ////////////////////////////////////////////////////////////////////// 108 { 109 Object source = actionEvent.getSource ( ); 110 111 if ( source == restoreButton ) 112 { 113 restoreButton.setEnabled ( false ); 114 115 updateButton.setEnabled ( false ); 116 117 agoracastInputPanel.useDefaults ( ); 118 119 reactToChanges = true; 120 } 121 else if ( source == updateButton ) 122 { 123 restoreButton.setEnabled ( false ); 124 125 updateButton.setEnabled ( false ); 126 127 AgoracastField [ ] agoracastFields 128 = agoracastMediator.getAgoracastFields ( ); 129 130 for ( int i = 0; i < agoracastFields.length; i++ ) 131 { 132 AgoracastField agoracastField = agoracastFields [ i ]; 133 134 String name = agoracastField.getName ( ); 135 136 String value = agoracastInputPanel.getValue ( name ); 137 138 if ( !ObjectLib.equivalent ( 139 value, agoracastField.getValue ( ) ) ) 140 { 141 AgoracastField newAgoracastField = new AgoracastField ( 142 name, 143 value, 144 agoracastField.getType ( ), 145 agoracastField.isReverse ( ), 146 agoracastField.getChoices ( ), 147 agoracastField.getSemantic ( ) ); 148 149 agoracastMediator.add ( newAgoracastField ); 150 } 151 } 152 153 agoracastInputPanel.useDefaults ( ); 154 155 reactToChanges = true; 156 } 157 } 158 159 ////////////////////////////////////////////////////////////////////// 160 ////////////////////////////////////////////////////////////////////// 161 }