1 16 17 package org.mc4j.console.connection.wizard; 18 19 import org.openide.WizardDescriptor; 20 import org.openide.util.NbBundle; 21 22 import javax.swing.event.ChangeEvent ; 23 import javax.swing.event.ChangeListener ; 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.NoSuchElementException ; 29 import java.util.Set ; 30 31 36 public class ConnectionIterator implements WizardDescriptor.Iterator { 37 38 private ConnectionDescriptor connectionDescriptor; 39 40 public ConnectionIterator() { 42 } 43 44 public void setDescriptor(ConnectionDescriptor connectionDescriptor) { 45 this.connectionDescriptor = connectionDescriptor; 46 } 47 48 protected WizardDescriptor.Panel[] createPanels() { 49 return new WizardDescriptor.Panel[] { 50 new ConnectionVisualPanel(this.connectionDescriptor), 51 new ServerInstallPanel(this.connectionDescriptor), 52 new ClassPathListPanel(this.connectionDescriptor) 53 }; 54 } 55 56 58 protected String [] createSteps() { 59 return new String [] { 60 "Connect to Server", 61 "Select Server Installation", 62 "Customize classpath" 63 }; 64 } 65 66 68 70 private transient int index = 0; 71 72 protected final int getIndex() { 73 return index; 74 } 75 private transient WizardDescriptor.Panel[] panels = null; 76 protected final WizardDescriptor.Panel[] getPanels() { 77 if (panels == null) { 78 panels = createPanels(); 79 } 80 return panels; 81 } 82 83 85 private transient String [] steps = null; 86 protected final String [] getSteps() { 87 if (steps == null) { 88 steps = createSteps(); 89 } 90 return steps; 91 } 92 93 98 public String name() { 99 return NbBundle.getMessage(ConnectionIterator.class, "TITLE_x_of_y", 100 new Integer (index + 1), new Integer (getPanels().length)); 101 } 102 103 private boolean showing (int index) throws NoSuchElementException { 104 switch (index) { 105 case 0: 106 return true; 107 case 1: 108 if (this.connectionDescriptor.getSettings().getConnectionType() == null) 109 return false; 110 return (this.connectionDescriptor.getSettings().getConnectionType().getConnectionClasspathEntries() != null); 111 case 2: 112 return true; 113 default: 114 throw new NoSuchElementException (); 115 } 116 } 117 public boolean hasNext () { 118 for (int i = index + 1; i < panels.length; i++) { 119 if (showing (i)) { 120 return true; 121 } 122 } 123 return false; 124 } 125 public boolean hasPrevious () { 126 return index > 0; 127 } 128 public void nextPanel () { 129 index++; 130 while (! showing (index)) index++; 131 if (index == 1) { 132 fireChangeEvent (); 134 } 135 } 136 public void previousPanel () { 137 index--; 138 while (! showing (index)) index--; 139 } 140 141 public WizardDescriptor.Panel current() { 142 return getPanels()[index]; 143 } 144 145 153 private transient Set listeners = new HashSet (1); public final void addChangeListener(ChangeListener l) { 155 synchronized (listeners) { 156 listeners.add(l); 157 } 158 } 159 public final void removeChangeListener(ChangeListener l) { 160 synchronized (listeners) { 161 listeners.remove(l); 162 } 163 } 164 protected final void fireChangeEvent() { 165 Iterator it; 166 synchronized (listeners) { 167 it = new HashSet (listeners).iterator(); 168 } 169 ChangeEvent ev = new ChangeEvent (this); 170 while (it.hasNext()) { 171 ((ChangeListener )it.next()).stateChanged(ev); 172 } 173 } 174 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 175 in.defaultReadObject(); 176 listeners = new HashSet (1); 177 } 178 179 180 } 181 | Popular Tags |