001 package com.croftsoft.apps.quiz; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import javax.swing.*; 006 007 // import com.croftsoft.core.gui.LabeledFieldsJPanel; 008 import com.croftsoft.core.gui.ButtonPanel2; 009 import com.croftsoft.core.lang.NullArgumentException; 010 011 /********************************************************************* 012 * 013 * <p /> 014 * 015 * @version 016 * 2001-07-11 017 * @since 018 * 2001-07-10 019 * @author 020 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 021 *********************************************************************/ 022 023 public final class QuizEditPanel 024 extends JPanel 025 implements ActionListener, Runnable 026 ////////////////////////////////////////////////////////////////////// 027 ////////////////////////////////////////////////////////////////////// 028 { 029 030 private final QuizModel quizModel; 031 032 private final JButton nextJButton = new JButton ( "Next" ); 033 034 private final JButton prevJButton = new JButton ( "Previous" ); 035 036 private final JButton saveJButton = new JButton ( "Save" ); 037 038 private final JButton [ ] jButtons 039 = new JButton [ ] { prevJButton, saveJButton, nextJButton }; 040 041 private final QuizItemPanel quizItemPanel; 042 043 // 044 045 private QuizItem [ ] quizItems; 046 047 private int quizItemIndex; 048 049 ////////////////////////////////////////////////////////////////////// 050 ////////////////////////////////////////////////////////////////////// 051 052 public QuizEditPanel ( 053 QuizModel quizModel, 054 Color panelBackgroundColor, 055 Color textFieldBackgroundColor ) 056 ////////////////////////////////////////////////////////////////////// 057 { 058 super ( new BorderLayout ( ), true ); // isDoubleBuffered 059 060 NullArgumentException.check ( this.quizModel = quizModel ); 061 062 // ignoring colors for now 063 064 quizItemPanel = new QuizItemPanel ( ); 065 066 Quiz quiz = quizModel.getQuiz ( ); 067 068 if ( quiz != null ) 069 { 070 quizItems = quiz.getQuizItems ( ); 071 } 072 else 073 { 074 throw new IllegalArgumentException ( "quizModel has null quiz" ); 075 } 076 077 if ( ( quizItems != null ) 078 && ( quizItems.length > 0 ) ) 079 { 080 quizItemPanel.setQuizItem ( quizItems [ 0 ] ); 081 } 082 083 add ( quizItemPanel, BorderLayout.CENTER ); 084 085 ButtonPanel2 buttonPanel2 = new ButtonPanel2 ( jButtons ); 086 087 for ( int i = 0; i < jButtons.length; i++ ) 088 { 089 JButton jButton = jButtons [ i ]; 090 091 jButton.setEnabled ( false ); 092 093 jButton.addActionListener ( this ); 094 } 095 096 if ( quizItems.length > 1 ) 097 { 098 nextJButton.setEnabled ( true ); 099 } 100 101 add ( buttonPanel2, BorderLayout.SOUTH ); 102 } 103 104 public QuizEditPanel ( QuizModel quizModel ) 105 ////////////////////////////////////////////////////////////////////// 106 { 107 this ( quizModel, null, null ); 108 } 109 110 ////////////////////////////////////////////////////////////////////// 111 ////////////////////////////////////////////////////////////////////// 112 113 public synchronized void actionPerformed ( ActionEvent actionEvent ) 114 ////////////////////////////////////////////////////////////////////// 115 { 116 Object source = actionEvent.getSource ( ); 117 118 if ( source == nextJButton ) 119 { 120 if ( quizItemIndex < quizItems.length - 1 ) 121 { 122 quizItemIndex++; 123 124 SwingUtilities.invokeLater ( this ); 125 } 126 127 nextJButton.setEnabled ( quizItemIndex < quizItems.length - 1 ); 128 129 prevJButton.setEnabled ( true ); 130 } 131 else if ( source == prevJButton ) 132 { 133 if ( quizItemIndex > 0 ) 134 { 135 quizItemIndex--; 136 137 SwingUtilities.invokeLater ( this ); 138 } 139 140 prevJButton.setEnabled ( quizItemIndex > 0 ); 141 142 nextJButton.setEnabled ( true ); 143 } 144 else 145 { 146 System.out.println ( source ); 147 } 148 } 149 150 ////////////////////////////////////////////////////////////////////// 151 ////////////////////////////////////////////////////////////////////// 152 153 public void run ( ) 154 ////////////////////////////////////////////////////////////////////// 155 { 156 quizItemPanel.setQuizItem ( quizItems [ quizItemIndex ] ); 157 } 158 159 ////////////////////////////////////////////////////////////////////// 160 ////////////////////////////////////////////////////////////////////// 161 }