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 java.io.IOException; 019 import java.util.ArrayList; 020 import java.util.Iterator; 021 import java.util.List; 022 023 import org.springframework.richclient.dialog.TitledPageApplicationDialog; 024 import org.springframework.richclient.dialog.TreeCompositeDialogPage; 025 import org.springframework.richclient.settings.Settings; 026 import org.springframework.util.Assert; 027 028 public class PreferenceDialog extends TitledPageApplicationDialog { 029 030 private List preferencePages = new ArrayList(); 031 032 private Settings settings; 033 034 public PreferenceDialog() { 035 this("preferenceDialog"); 036 } 037 038 public PreferenceDialog(String dialogId) { 039 super(new TreeCompositeDialogPage(dialogId)); 040 } 041 042 private void addPage(PreferencePage page) { 043 Assert.isTrue(!isControlCreated(), "Add pages before control is created."); 044 preferencePages.add(page); 045 page.setPreferenceDialog(this); 046 } 047 048 public void addPreferencePage(PreferencePage page) { 049 addPage(page); 050 getPageContainer().addPage(page); 051 } 052 053 public void addPreferencePage(PreferencePage parent, PreferencePage page) { 054 addPage(page); 055 getPageContainer().addPage(parent, page); 056 } 057 058 private TreeCompositeDialogPage getPageContainer() { 059 return (TreeCompositeDialogPage) getDialogPage(); 060 } 061 062 public Settings getSettings() { 063 return settings; 064 } 065 066 public boolean onFinish() { 067 for (Iterator iter = preferencePages.iterator(); iter.hasNext();) { 068 PreferencePage page = (PreferencePage) iter.next(); 069 // give page the chance to veto 070 if (!page.onFinish()) { 071 return false; 072 } 073 } 074 if (settings != null) { 075 try { 076 settings.save(); 077 } catch (IOException e) { 078 // TODO handle exception 079 e.printStackTrace(); 080 return false; 081 } 082 } 083 084 return true; 085 } 086 087 public void setSettings(Settings settings) { 088 Assert.notNull(settings, "Settings cannot be null."); 089 this.settings = settings; 090 } 091 }