001package jmri.jmrit.logixng.implementation; 002 003import java.util.*; 004 005import jmri.*; 006import jmri.jmrit.logixng.*; 007 008/** 009 * Default implementation of the clipboard 010 * 011 * @author Daniel Bergqvist (C) 2020 012 */ 013public class DefaultClipboard extends AbstractBase implements Clipboard { 014 015 private ClipboardMany _clipboardItems = new ClipboardMany("", null); 016 017 private final FemaleAnySocket _femaleSocket = new DefaultFemaleAnySocket(this, new FemaleSocketListener() { 018 @Override 019 public void connected(FemaleSocket socket) { 020 // Do nothing 021 } 022 023 @Override 024 public void disconnected(FemaleSocket socket) { 025 // Do nothing 026 } 027 }, "A"); 028 029 030 public DefaultClipboard() { 031 super("IQClipboard"); 032 033 // Listeners should never be enabled for the clipboard 034 _femaleSocket.setEnableListeners(false); 035 036 try { 037 _femaleSocket.connect(new MaleRootSocket(null)); 038 } catch (SocketAlreadyConnectedException ex) { 039 // This should never happen 040 throw new RuntimeException("Program error", ex); 041 } 042 if (!_femaleSocket.setParentForAllChildren(new ArrayList<>())) { 043 throw new RuntimeException("Failed to set parent for all children"); 044 } 045 _clipboardItems.setParent(_femaleSocket.getConnectedSocket()); 046 } 047 048 @Override 049 public boolean isEmpty() { 050 return _clipboardItems.getChildCount() == 0; 051 } 052 053 @Override 054 public boolean add(MaleSocket maleSocket, List<String> errors) { 055 _clipboardItems.ensureFreeSocketAtTop(); 056 try { 057 _clipboardItems.getChild(0).connect(maleSocket); 058 return _clipboardItems.setParentForAllChildren(errors); 059 } catch (SocketAlreadyConnectedException ex) { 060 throw new RuntimeException("Cannot add socket", ex); 061 } 062 } 063 064 @Override 065 public MaleSocket fetchTopItem() { 066 if (!isEmpty()) { 067 MaleSocket maleSocket = _clipboardItems.getChild(0).getConnectedSocket(); 068 _clipboardItems.getChild(0).disconnect(); 069 return maleSocket; 070 } else { 071 return null; 072 } 073 } 074 075 @Override 076 public MaleSocket getTopItem() { 077 if (!isEmpty()) { 078 return _clipboardItems.getChild(0).getConnectedSocket(); 079 } else { 080 return null; 081 } 082 } 083 084 @Override 085 public FemaleSocket getFemaleSocket() { 086 return _femaleSocket; 087 } 088 089 @Override 090 public void moveItemToTop(MaleSocket maleSocket) { 091 _clipboardItems.ensureFreeSocketAtTop(); 092 if (maleSocket.getParent() != null) { 093 if (!(maleSocket.getParent() instanceof FemaleSocket)) { 094 throw new UnsupportedOperationException("maleSocket.parent() is not a female socket"); 095 } 096 ((FemaleSocket)maleSocket.getParent()).disconnect(); 097 } 098 try { 099 _clipboardItems.getChild(0).connect(maleSocket); 100 } catch (SocketAlreadyConnectedException ex) { 101 throw new UnsupportedOperationException("Cannot move item to clipboard", ex); 102 } 103 } 104 105 @Override 106 public void setup() { 107 _clipboardItems.setup(); 108 } 109 110 public boolean replaceClipboardItems(ClipboardMany clipboardItems, List<String> errors) { 111 _clipboardItems = clipboardItems; 112 113 _femaleSocket.disconnect(); 114 115 try { 116 _femaleSocket.connect(new MaleRootSocket(null)); 117 } catch (SocketAlreadyConnectedException ex) { 118 // This should never happen 119 throw new RuntimeException("Program error", ex); 120 } 121 boolean result = _femaleSocket.setParentForAllChildren(errors); 122 _clipboardItems.setParent(_femaleSocket.getConnectedSocket()); 123 return result; 124 } 125 126 @Override 127 protected void registerListenersForThisClass() { 128 throw new UnsupportedOperationException("Not supported"); 129 } 130 131 @Override 132 protected void unregisterListenersForThisClass() { 133 throw new UnsupportedOperationException("Not supported"); 134 } 135 136 @Override 137 protected void disposeMe() { 138 throw new UnsupportedOperationException("Not supported"); 139 } 140 141 @Override 142 public void setState(int s) throws JmriException { 143 throw new UnsupportedOperationException("Not supported"); 144 } 145 146 @Override 147 public int getState() { 148 throw new UnsupportedOperationException("Not supported"); 149 } 150 151 @Override 152 public String getBeanType() { 153 throw new UnsupportedOperationException("Not supported"); 154 } 155 156 @Override 157 public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws JmriException { 158 throw new UnsupportedOperationException("Not supported"); 159 } 160 161 @Override 162 public String getShortDescription(Locale locale) { 163 throw new UnsupportedOperationException("Not supported"); 164 } 165 166 @Override 167 public String getLongDescription(Locale locale) { 168 throw new UnsupportedOperationException("Not supported"); 169 } 170 171 @Override 172 public Base getParent() { 173 return null; 174 } 175 176 @Override 177 public void setParent(Base parent) { 178 throw new UnsupportedOperationException("Not supported"); 179 } 180 181 @Override 182 public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException { 183 throw new UnsupportedOperationException("Not supported"); 184 } 185 186 @Override 187 public int getChildCount() { 188 throw new UnsupportedOperationException("Not supported"); 189 } 190 191 @Override 192 public Category getCategory() { 193 throw new UnsupportedOperationException("Not supported"); 194 } 195 196 197 private class MaleRootSocket extends AbstractMaleSocket { 198 199 public MaleRootSocket(BaseManager<? extends NamedBean> manager) { 200 super(manager, _clipboardItems); 201 } 202 203 @Override 204 protected void registerListenersForThisClass() { 205 throw new UnsupportedOperationException("Not supported"); 206 } 207 208 @Override 209 protected void unregisterListenersForThisClass() { 210 throw new UnsupportedOperationException("Not supported"); 211 } 212 213 @Override 214 protected void disposeMe() { 215 _clipboardItems.dispose(); 216 } 217 218 @Override 219 public void setEnabled(boolean enable) { 220 throw new UnsupportedOperationException("Not supported"); 221 } 222 223 @Override 224 public void setEnabledFlag(boolean enable) { 225 throw new UnsupportedOperationException("Not supported"); 226 } 227 228 @Override 229 public boolean isEnabled() { 230 return _clipboardItems.isEnabled(); 231 } 232 233 @Override 234 public void setDebugConfig(DebugConfig config) { 235 throw new UnsupportedOperationException("Not supported"); 236 } 237 238 @Override 239 public DebugConfig getDebugConfig() { 240 return null; 241 } 242 243 @Override 244 public DebugConfig createDebugConfig() { 245 throw new UnsupportedOperationException("Not supported"); 246 } 247 248 @Override 249 public String getComment() { 250 return _clipboardItems.getComment(); 251 } 252 253 @Override 254 public void setComment(String s) throws NamedBean.BadUserNameException { 255 throw new UnsupportedOperationException("Not supported"); 256 } 257 258 } 259 260}