1
22 package org.crosswire.bibledesktop.book.install;
23
24 import java.awt.BorderLayout;
25 import java.awt.Component;
26 import java.awt.FlowLayout;
27 import java.awt.Frame;
28 import java.awt.event.WindowAdapter;
29 import java.awt.event.WindowEvent;
30 import java.io.IOException;
31 import java.io.ObjectInputStream;
32 import java.util.Map;
33
34 import javax.swing.BorderFactory;
35 import javax.swing.JButton;
36 import javax.swing.JDialog;
37 import javax.swing.JOptionPane;
38 import javax.swing.JPanel;
39 import javax.swing.JTabbedPane;
40
41 import org.crosswire.bibledesktop.BDMsg;
42 import org.crosswire.common.progress.swing.JobsProgressBar;
43 import org.crosswire.common.swing.ActionFactory;
44 import org.crosswire.common.swing.GuiUtil;
45 import org.crosswire.common.swing.desktop.LayoutPersistence;
46 import org.crosswire.jsword.book.install.InstallManager;
47 import org.crosswire.jsword.book.install.Installer;
48 import org.crosswire.jsword.book.install.InstallerEvent;
49 import org.crosswire.jsword.book.install.InstallerListener;
50
51
65 public class SitesPane extends JPanel {
66
69 public SitesPane() {
70 init();
71
72 imanager = new InstallManager();
73 installers = imanager.getInstallers();
74
75 addAllInstallers();
76
77 imanager.addInstallerListener(new SiteInstallerListener());
78 GuiUtil.applyDefaultOrientation(this);
79 }
80
81
84 private void init() {
85 actions = new ActionFactory(this);
86
87 tabMain = new JTabbedPane();
88 this.setLayout(new BorderLayout());
89 this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
90 this.add(tabMain, BorderLayout.CENTER);
91 this.add(new JobsProgressBar(true), BorderLayout.SOUTH);
92 }
93
94
97 protected final void addAllInstallers() {
98 for (String name : installers.keySet()) {
100 Installer installer = installers.get(name);
101
102 SitePane site = new SitePane(installer);
103 tabMain.add(name, site);
104 }
105
106 tabMain.add(BDMsg.gettext("Installed Books"), new SitePane());
108 }
109
110
113 protected void removeAllInstallers() {
114 tabMain.removeAll();
115 }
116
117
120 public void doManageSites() {
121 EditSitePane edit = new EditSitePane(imanager);
122 edit.showInDialog(this);
123 }
124
125
128 public void doSitesClose() {
129 if (dlgMain != null) {
130 LayoutPersistence.instance().saveLayout(dlgMain);
131 dlgMain.setVisible(false);
132 }
133 }
134
135
138 public void showInDialog(Component parent) {
139 Frame root = JOptionPane.getFrameForComponent(parent);
140 dlgMain = new JDialog(root);
141 dlgMain.getContentPane().setLayout(new BorderLayout());
142 dlgMain.getContentPane().add(this, BorderLayout.CENTER);
143 dlgMain.getContentPane().add(createButtons(), BorderLayout.SOUTH);
144 dlgMain.setTitle(BDMsg.gettext("Available Books"));
146 dlgMain.setResizable(true);
147 dlgMain.setName("Sites");
150 dlgMain.addWindowListener(new WindowAdapter() {
151
154 @Override
155 public void windowClosed(WindowEvent ev) {
156 doSitesClose();
157 }
158 });
159 dlgMain.setLocationRelativeTo(parent);
160
161 LayoutPersistence layoutPersistence = LayoutPersistence.instance();
164 if (layoutPersistence.isLayoutPersisted(dlgMain)) {
165 layoutPersistence.restoreLayout(dlgMain);
166 } else {
167 dlgMain.setSize(750, 500);
168 GuiUtil.centerOnScreen(dlgMain);
169 }
170
171 dlgMain.setVisible(true);
172 dlgMain.toFront();
173 GuiUtil.applyDefaultOrientation(dlgMain);
174 }
175
176
179 private Component createButtons() {
180 if (pnlButtons == null) {
181 JButton btnOK = new JButton(actions.addAction("SitesClose", BDMsg.gettext("OK")));
183
184 JButton btnAdd = new JButton(actions.addAction("ManageSites", BDMsg.gettext("Edit Sites ...")));
186
187 pnlButtons = new JPanel();
188 pnlButtons.setLayout(new FlowLayout(FlowLayout.TRAILING));
189 pnlButtons.add(btnAdd, null);
190 pnlButtons.add(btnOK);
191 }
192 return pnlButtons;
193
194 }
195
196
203 private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
204 actions = new ActionFactory(this);
205 imanager = new InstallManager();
206 installers = imanager.getInstallers();
207
208 is.defaultReadObject();
209
210 addAllInstallers();
211
212 imanager.addInstallerListener(new SiteInstallerListener());
213 }
214
215
218 class SiteInstallerListener implements InstallerListener {
219
220
223 public void installerAdded(InstallerEvent ev) {
224 Installer installer = ev.getInstaller();
225 String name = imanager.getInstallerNameForInstaller(installer);
226
227 SitePane site = new SitePane(installer);
228 tabMain.add(name, site);
229 }
230
231
234 public void installerRemoved(InstallerEvent ev) {
235 removeAllInstallers();
242 addAllInstallers();
243 }
244 }
245
246
249 private transient Map<String, Installer> installers;
250
251
254 protected transient InstallManager imanager;
255
256 private transient ActionFactory actions;
257
258
261 private JDialog dlgMain;
262 private JPanel pnlButtons;
263 protected JTabbedPane tabMain;
264
265
268 private static final long serialVersionUID = 3258126947069605936L;
269 }
270