Created
December 10, 2012 12:29
-
-
Save vomindoraan/4250287 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BrushListDialog extends JDialog { | |
// ... | |
private static class BrushListCellRenderer extends DefaultListCellRenderer { | |
private static final Color BG = new Color(0xFFFFF0); | |
private static final Color BG_SELECTED = new Color(0x39697B); | |
@Override | |
public Component getListCellRendererComponent(JList<?> list, Object value, int index, | |
boolean isSelected, boolean cellHasFocus) { | |
Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); | |
if (!TYPES.contains(value)) { | |
comp.setBackground(isSelected ? BG_SELECTED : BG); | |
} | |
return comp; | |
} | |
} | |
private class BrushListModel extends DefaultListModel<String> { | |
@Override | |
public void addElement(String elem) { | |
elem = formatClassName(elem); | |
if (!contains(elem) && !elem.matches("|worldspawn")) { | |
super.addElement(elem); | |
} | |
} | |
@Override | |
public boolean removeElement(Object elem) { | |
if (TYPES.contains(elem) && !showRemoveConfirmDialog(elem)) { | |
return false; | |
} | |
return super.removeElement(elem); | |
} | |
@Override | |
public int indexOf(Object elem) { | |
return super.indexOf(formatClassName((String) elem)); | |
} | |
private String formatClassName(String elem) { | |
return elem.trim().toLowerCase().replace(' ', '_').replaceAll("[^\\w]", ""); | |
} | |
private boolean showRemoveConfirmDialog(Object elem) { | |
int option = JOptionPane.showConfirmDialog(BrushListDialog.this, | |
elem + " is a default brush type.\nDo you want to allow the removal of such entries?", | |
"Remove", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); | |
if (option == JOptionPane.YES_OPTION) { | |
TYPES.clear(); | |
return true; | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment