001: package net.xoetrope.builder.editor;
002:
003: import java.awt.BorderLayout;
004: import java.util.Enumeration;
005: import java.util.Hashtable;
006: import java.util.Vector;
007:
008: import java.awt.Dimension;
009: import javax.swing.DefaultListModel;
010: import javax.swing.JList;
011: import javax.swing.JPanel;
012: import javax.swing.event.ListSelectionEvent;
013: import javax.swing.event.ListSelectionListener;
014:
015: import net.xoetrope.builder.editor.events.ProjectListener;
016: import net.xoetrope.builder.editor.events.XPageListListener;
017: import java.awt.Font;
018: import javax.swing.JScrollPane;
019: import javax.swing.border.EmptyBorder;
020:
021: /**
022: * <p>An editor for the XUI project</p>
023: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
024: * $Revision: 1.12 $
025: */
026: public class XPageList extends JPanel implements ListSelectionListener,
027: ProjectListener {
028: private JList list;
029: private XPageListListener pageListListener;
030: private XEditorProject currentProject;
031: private XPageResource selectedPage;
032:
033: /**
034: * Create a new list of pages.
035: */
036: public XPageList() {
037: setLayout(new BorderLayout());
038:
039: list = new JList();
040: list
041: .setSelectionMode(list.getSelectionModel().SINGLE_SELECTION);
042: list.addListSelectionListener(this );
043: list.setFont(XuiDefaults.defaultFont);
044:
045: JScrollPane sp = new JScrollPane(list);
046: sp.setBorder(new EmptyBorder(0, 0, 0, 0));
047: add(sp, BorderLayout.CENTER);
048: }
049:
050: /**
051: * Remove all of the screens and re-populate from the current project.
052: */
053: public void projectUpdated() {
054: list.removeAll();
055: Hashtable table = currentProject.getPageResources();
056: Vector v = new Vector(table.size());
057: Enumeration e = table.keys();
058:
059: DefaultListModel mdl = new DefaultListModel();
060: while (e.hasMoreElements()) {
061: mdl.addElement(e.nextElement());
062: }
063: list.setModel(mdl);
064: list.updateUI();
065:
066: if (table.size() > 0) {
067: if (selectedPage != null) {
068: list.setSelectedValue(selectedPage, true);
069: if (pageListListener != null)
070: pageListListener.loadPage(selectedPage.getName(),
071: null, 0, 0);
072: } else {
073: selectedPage = currentProject.getPageResource(list
074: .getModel().getElementAt(0).toString());
075: list.setSelectedValue(selectedPage, true);
076: if (pageListListener != null)
077: pageListListener.loadPage(selectedPage.getName(),
078: null, 0, 0);
079: }
080: }
081: }
082:
083: /**
084: * Get the preferred size of this component
085: * @return the minimum size
086: */
087: public Dimension getPreferredSize() {
088: return new Dimension(230, 100);
089: }
090:
091: /**
092: * If the projectListener is not null call the loadScreen function, passing
093: * the selected screen.
094: * @param e
095: */
096: public void valueChanged(ListSelectionEvent e) {
097: if (!e.getValueIsAdjusting()) {
098: if (pageListListener != null) {
099: if (list.getSelectedValue() != null)
100: pageListListener.loadPage(list.getSelectedValue()
101: .toString(), null, 0, 0);
102: }
103: }
104: }
105:
106: /**
107: * Set the ProjectListener
108: * @param p the new ProjectListener
109: */
110: public void addPageListListener(XPageListListener p) {
111: pageListListener = p;
112: }
113:
114: /**
115: * Get the selected screen name from the XList
116: * @return the name of the selected screen
117: */
118: public String getSelectedPage() {
119: return (String) list.getSelectedValue();
120: }
121:
122: /**
123: * Select a page
124: * @param pageName the page to select
125: */
126: public void setSelectedPage(XPageResource page) {
127: selectedPage = page;
128: list.setSelectedValue(page, true);
129: }
130:
131: /**
132: * The project has been loaded
133: * @param project the new project reference
134: */
135: public void projectLoaded(XEditorProject project) {
136: currentProject = project;
137: projectUpdated();
138: }
139: }
|