Skip to content

Instantly share code, notes, and snippets.

@fiskurgit
Created November 4, 2013 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiskurgit/7301984 to your computer and use it in GitHub Desktop.
Save fiskurgit/7301984 to your computer and use it in GitHub Desktop.
Display colors from an Android Colors.xml content
package com.fiskur.androidcolours;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Scanner;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class AndroidColours extends javax.swing.JFrame {
private javax.swing.JList coloursList;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea textArea;
private final DefaultListModel colorListModel = new DefaultListModel();
public AndroidColours() {
initComponents();
coloursList.setCellRenderer(new ColorCellRenderer());
textArea.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
getColors();
}
@Override
public void removeUpdate(DocumentEvent e) {
getColors();
}
@Override
public void changedUpdate(DocumentEvent e) {
getColors();
}
});
}
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
textArea = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
coloursList = new javax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textArea.setColumns(20);
textArea.setRows(5);
jScrollPane1.setViewportView(textArea);
coloursList.setModel(colorListModel);
jScrollPane2.setViewportView(coloursList);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}
private void getColors(){
colorListModel.clear();
String colorXML = textArea.getText();
Scanner scanner = new Scanner(colorXML);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.contains("<color name=")){
colorListModel.addElement(line);
}
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new AndroidColours().setVisible(true);
}
});
}
class ColorCellRenderer extends JLabel implements ListCellRenderer {
public ColorCellRenderer() {
setOpaque(true);
setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
}
@Override
public Component getListCellRendererComponent(JList paramlist, Object value, int index, boolean isSelected, boolean cellHasFocus) {
String content = value.toString();
if(content.contains("<color name=\"")){
String label = content.substring(content.indexOf("name=\"") + 6, content.indexOf("\">"));
String color = content.substring(content.indexOf("\">") + 3, content.indexOf("</color>"));
Color col = Color.decode("0x" + color);
if(col.getRGB() == -1){
setForeground(Color.decode("0x666666"));
}else{
setForeground(Color.decode("0xffffff"));
}
setBackground(col);
setText("#" + color + ": " + label);
}else{
setBackground(Color.GRAY);
setText(value.toString());
}
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment