cs106a – Assignment #6 – Extra Task #2

The complete specification of assignment #6 can be found as part of the stream at iTunes.

Allow deletion as well as addition

Because the screen quickly becomes cluttered as you graph lots of names, it would be convenient if there were some way to delete entries individually, as opposed to clearing the entire display and then adding back the ones you wanted. The obvious strategy would be to add a Delete button that eliminated the entry corresponding to the value in the Name box. That approach, however, has a minor drawback given the design so far. If you added a bunch of entries to the graph and then deleted the early ones, the colors of the later entries would shift, which might prove disconcerting. Can you redesign the color-selection strategy so that displayed entries retain their color even if other entries are removed?

Add the delete button to the main class:

	private JButton deleteButton;

	public void init() {
		...
		deleteButton = new JButton("Delete");
		...
		add(deleteButton, POSITION_OF_INTERACTORS);
		...
	}

	public void actionPerformed(ActionEvent e) {
		...
		} else if (source == deleteButton) {
			graph.removeEntry(dataBase.findEntry(nameField.getText()));
		}
	}

Removing the the entry is a single line, but it now the top maps needs adjusting:

	public void removeEntry(NameSurferEntry entry) {
		if (!data.contains(entry)) return;
		data.remove(entry);		
		String name = entry.getName();
		for (int i = 0; i < NDECADES; i++) {
			if (!topRank.containsKey(i)) continue;
			if (!name.equals(topName.get(i))) continue;
			topRank.remove(i);
			topName.remove(i);
			Iterator<NameSurferEntry> it = data.iterator();
			while (it.hasNext()) {
				addTopRank(it.next(), i);
			}
		}
		update();
	}

	private void addTopRank(NameSurferEntry entry, int i) {
		int rank = entry.getRank(i);
		if (rank == 0) return;
		if (!topRank.containsKey(i)) {
			topRank.put(i, rank);
			topName.put(i, entry.getName());
		} else if (rank < topRank.get(i)) {
			topRank.put(i, rank);
			topName.put(i, entry.getName());				
		}
	}

To keep the colors and the markers constant create new maps to hold them, and two helper methods to provide new colors & markers:

	private HashMap<String,Color> colors = new HashMap<String,Color>();
	private int colorIndex = 0;
	private HashMap<String,Marker> markers = new HashMap<String,Marker>();
	private int markerIndex = 0;

	private Color nextColor() {
		return COLORS[colorIndex++ % N_COLORS];	
	}

	private Marker nextMarker() {
		return Marker.values()[markerIndex++  % N_MARKERS];	
	}

They are set up when an entry is added:

	public void addEntry(NameSurferEntry entry) {
		...
		String name = entry.getName();
		colors.put(name, nextColor());
		markers.put(name, nextMarker());	
		update();
	}

… and are removed with the entry:

	public void removeEntry(NameSurferEntry entry) {
		...
		colors.remove(name);
		markers.remove(name);
		update();
	}

… and are used to draw an entry:

	private void drawEntry(NameSurferEntry entry) {
		...
		Color color = colors.get(name);
		Marker marker = markers.get(name);
		...
	}

The code for this assignment is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.