001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.preference; 017 018 import javax.swing.JComponent; 019 020 import org.springframework.richclient.command.ActionCommand; 021 import org.springframework.richclient.command.CommandGroup; 022 import org.springframework.richclient.dialog.AbstractDialogPage; 023 import org.springframework.richclient.layout.GridBagLayoutBuilder; 024 import org.springframework.richclient.settings.Settings; 025 import org.springframework.richclient.util.GuiStandardUtils; 026 import org.springframework.util.Assert; 027 028 public abstract class PreferencePage extends AbstractDialogPage { 029 030 private boolean createApplyAndDefaultButtons = true; 031 032 private PreferencePage parent; 033 034 private PreferenceDialog preferenceDialog; 035 036 private ActionCommand restoreDefaultsCommand; 037 038 private ActionCommand applyCommand; 039 040 public PreferencePage(String id) { 041 super(id); 042 } 043 044 public PreferencePage(String id, boolean autoconfigure) { 045 super(id, autoconfigure); 046 } 047 048 /** 049 * @return array containing "Restore defaults" and "Apply" commands 050 */ 051 protected ActionCommand[] getCommands() { 052 return new ActionCommand[] { getRestoreDefaultsCommand(), getApplyCommand() }; 053 } 054 055 /** 056 * Will create "Apply" command if it doesn't exist yet 057 * 058 * @return apply command. 059 */ 060 protected ActionCommand getApplyCommand() 061 { 062 if (applyCommand == null) { 063 applyCommand = new ActionCommand("applyCommand") { 064 public void doExecuteCommand() { 065 onApply(); 066 } 067 }; 068 } 069 return applyCommand; 070 } 071 072 /** 073 * Will create "Restore Defaults" command if it doesn't exist yet 074 * 075 * @return restore defaults command. 076 */ 077 protected ActionCommand getRestoreDefaultsCommand() 078 { 079 if (restoreDefaultsCommand == null){ 080 restoreDefaultsCommand = new ActionCommand("restoreDefaultsCommand") { 081 public void doExecuteCommand() { 082 onDefaults(); 083 } 084 }; 085 } 086 return restoreDefaultsCommand; 087 } 088 089 /** 090 * Creates two commands "Restore defaults" and "Apply" for this page, 091 * layouts them on the panel. 092 * 093 * @return panel containing "Restore defaults" and "Apply" commands 094 */ 095 protected JComponent createButtons() { 096 CommandGroup commandGroup = CommandGroup.createCommandGroup(null, 097 getCommands()); 098 JComponent buttonBar = commandGroup.createButtonBar(); 099 GuiStandardUtils.attachDialogBorder(buttonBar); 100 101 return buttonBar; 102 } 103 104 protected abstract JComponent createContents(); 105 106 protected JComponent createControl() { 107 GridBagLayoutBuilder builder = new GridBagLayoutBuilder(); 108 109 JComponent buttonPanel = null; 110 if (createApplyAndDefaultButtons) { 111 buttonPanel = createButtons(); 112 } 113 114 JComponent contents = createContents(); 115 Assert.notNull(contents, "Contents cannot be null."); 116 builder.append(contents, 1, 1, true, true); 117 118 if (createApplyAndDefaultButtons) { 119 builder.nextLine(); 120 builder.append(buttonPanel); 121 } 122 123 return builder.getPanel(); 124 } 125 126 public PreferencePage getParent() { 127 return parent; 128 } 129 130 protected Settings getSettings() { 131 return preferenceDialog.getSettings(); 132 } 133 134 /** 135 * Must store the preference values in the PreferenceStore. Does not save 136 * the PreferenceStore. Subclasses should override this method. 137 */ 138 protected void onApply() { 139 onFinish(); 140 } 141 142 protected void onDefaults() { 143 } 144 145 /** 146 * Notification that the user clicked the OK button on the PreferenceDialog. 147 */ 148 protected boolean onFinish() { 149 return true; 150 } 151 152 public void setCreateApplyAndDefaultButtons(boolean create) { 153 createApplyAndDefaultButtons = create; 154 } 155 156 public boolean getCreateApplyAndDefaultButtons() { 157 return createApplyAndDefaultButtons; 158 } 159 160 public void setParent(PreferencePage parent) { 161 this.parent = parent; 162 } 163 164 public void setPreferenceDialog(PreferenceDialog dialog) { 165 Assert.notNull(dialog); 166 preferenceDialog = dialog; 167 } 168 169 public void setPageComplete(boolean pageComplete) { 170 if (applyCommand != null) { 171 applyCommand.setEnabled(pageComplete); 172 } 173 174 super.setPageComplete(pageComplete); 175 } 176 }