File: Projects.java

package info (click to toggle)
logisim 2.7.1~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 14,112 kB
  • ctags: 11,220
  • sloc: java: 66,040; xml: 1,113; haskell: 342; makefile: 22
file content (174 lines) | stat: -rw-r--r-- 5,190 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Copyright (c) 2010, Carl Burch. License information is located in the
 * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */

package com.cburch.logisim.proj;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.WeakHashMap;

import com.cburch.logisim.file.Loader;
import com.cburch.logisim.gui.main.Frame;
import com.cburch.logisim.util.PropertyChangeWeakSupport;

public class Projects {
	public static final String projectListProperty = "projectList";
	
	private static final WeakHashMap<Window, Point> frameLocations
		= new WeakHashMap<Window, Point>();
	
	private static void projectRemoved(Project proj, Frame frame,
			MyListener listener) {
		frame.removeWindowListener(listener);
		openProjects.remove(proj);
		proj.getSimulator().shutDown();
		propertySupport.firePropertyChange(projectListProperty, null, null);
	}

	private static class MyListener extends WindowAdapter {
		@Override
		public void windowActivated(WindowEvent event) {
			mostRecentFrame = (Frame) event.getSource();
		}
		
		@Override
		public void windowClosing(WindowEvent event) {
			Frame frame = (Frame) event.getSource();
			if ((frame.getExtendedState() & Frame.ICONIFIED) == 0) {
				mostRecentFrame = frame;
				try {
					frameLocations.put(frame, frame.getLocationOnScreen());
				} catch (Throwable t) { }
			}
		}
		
		@Override
		public void windowClosed(WindowEvent event) {
			Frame frame = (Frame) event.getSource();
			Project proj = frame.getProject();
			
			if (frame == proj.getFrame()) {
				projectRemoved(proj, frame, this);
			}
			if (openProjects.isEmpty()) {
				ProjectActions.doQuit();
			}
		}
		
		@Override
		public void windowOpened(WindowEvent event) {
			Frame frame = (Frame) event.getSource();
			Project proj = frame.getProject();

			if (frame == proj.getFrame() && !openProjects.contains(proj)) {
				openProjects.add(proj);
				propertySupport.firePropertyChange(projectListProperty, null, null);
			}
		}
	}

	private static final MyListener myListener = new MyListener();
	private static final PropertyChangeWeakSupport propertySupport
		= new PropertyChangeWeakSupport(Projects.class);
	private static ArrayList<Project> openProjects = new ArrayList<Project>();
	private static Frame mostRecentFrame = null;

	private Projects() { }
	
	public static Frame getTopFrame() {
		Frame ret = mostRecentFrame;
		if (ret == null) {
			Frame backup = null;
			for (Project proj : openProjects) {
				Frame frame = proj.getFrame();
				if (ret == null) ret = frame;
				if (ret.isVisible() && (ret.getExtendedState() & Frame.ICONIFIED) != 0) {
					backup = ret;
				}
			}
			if (ret == null) ret = backup;
		}
		return ret;
	}
	
	static void windowCreated(Project proj, Frame oldFrame, Frame frame) {
		if (oldFrame != null) {
			projectRemoved(proj, oldFrame, myListener);
		}

		if (frame == null) return;
		
		// locate the window
		Point lowest = null;
		for (Project p : openProjects) {
			Frame f = p.getFrame();
			if (f == null) continue;
			Point loc = p.getFrame().getLocation();
			if (lowest == null || loc.y > lowest.y) lowest = loc;
		}
		if (lowest != null) {
			Dimension sz = frame.getToolkit().getScreenSize();
			int x = Math.min(lowest.x + 20, sz.width - 200);
			int y = Math.min(lowest.y + 20, sz.height - 200);
			if (x < 0) x = 0;
			if (y < 0) y = 0;
			frame.setLocation(x, y);
		}

		if (frame.isVisible() && !openProjects.contains(proj)) {
			openProjects.add(proj);
			propertySupport.firePropertyChange(projectListProperty, null, null);
		}
		frame.addWindowListener(myListener);
	}
	
	public static List<Project> getOpenProjects() {
		return Collections.unmodifiableList(openProjects);
	}
	
	public static boolean windowNamed(String name) {
		for (Project proj : openProjects) {
			if (proj.getLogisimFile().getName().equals(name)) return true;
		}
		return false;
	}
	
	public static Project findProjectFor(File query) {
		for (Project proj : openProjects) {
			Loader loader = proj.getLogisimFile().getLoader();
			if (loader == null) continue;
			File f = loader.getMainFile();
			if (query.equals(f)) return proj;
		}
		return null;
	}

	//
	// PropertyChangeSource methods
	//
	public static void addPropertyChangeListener(PropertyChangeListener listener) {
		propertySupport.addPropertyChangeListener(listener);
	}
	public static void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
		propertySupport.addPropertyChangeListener(propertyName, listener);
	}
	public static void removePropertyChangeListener(PropertyChangeListener listener) {
		propertySupport.removePropertyChangeListener(listener);
	}
	public static void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
		propertySupport.removePropertyChangeListener(propertyName, listener);
	}
	
	public static Point getLocation(Window win) {
		Point ret = frameLocations.get(win);
		return ret == null ? null : (Point) ret.clone();
	}
}