1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.gui;
22 
23 import java.awt.Color;
24 
25 import javax.swing.JComponent;
26 import javax.swing.JLabel;
27 import javax.swing.JTable;
28 import javax.swing.UIManager;
29 
30 import net.sourceforge.atunes.model.IAudioObject;
31 import net.sourceforge.atunes.model.IColumnSet;
32 import net.sourceforge.atunes.model.INavigationHandler;
33 import net.sourceforge.atunes.model.IStateNavigation;
34 import net.sourceforge.atunes.model.ITableCellRendererCode;
35 import net.sourceforge.atunes.model.ITagHandler;
36 
37 /**
38  * Renderer for navigation table, includes highlight of incomplete tags
39  *
40  * @author alex
41  *
42  */
43 public class NavigationTableCellRendererCode extends
44 		AbstractTableCellRendererCode<JComponent, Object> {
45 
46 	private ITableCellRendererCode<JComponent, Object> renderer;
47 
48 	private INavigationHandler navigationHandler;
49 
50 	private boolean isSubstance;
51 
52 	private ITagHandler tagHandler;
53 
54 	private IStateNavigation stateNavigation;
55 
56 	private IColumnSet navigatorColumnSet;
57 
58 	/**
59 	 * @param navigatorColumnSet
60 	 */
setNavigatorColumnSet(final IColumnSet navigatorColumnSet)61 	public void setNavigatorColumnSet(final IColumnSet navigatorColumnSet) {
62 		this.navigatorColumnSet = navigatorColumnSet;
63 	}
64 
65 	/**
66 	 * @param renderer
67 	 */
setRenderer( final ITableCellRendererCode<JComponent, Object> renderer)68 	public void setRenderer(
69 			final ITableCellRendererCode<JComponent, Object> renderer) {
70 		this.renderer = renderer;
71 	}
72 
73 	/**
74 	 * @param navigationHandler
75 	 */
setNavigationHandler(final INavigationHandler navigationHandler)76 	public void setNavigationHandler(final INavigationHandler navigationHandler) {
77 		this.navigationHandler = navigationHandler;
78 	}
79 
80 	/**
81 	 * @param tagHandler
82 	 */
setTagHandler(final ITagHandler tagHandler)83 	public void setTagHandler(final ITagHandler tagHandler) {
84 		this.tagHandler = tagHandler;
85 	}
86 
87 	/**
88 	 * @param stateNavigation
89 	 */
setStateNavigation(final IStateNavigation stateNavigation)90 	public void setStateNavigation(final IStateNavigation stateNavigation) {
91 		this.stateNavigation = stateNavigation;
92 	}
93 
94 	/**
95 	 * Initialization needed
96 	 */
initialize()97 	public void initialize() {
98 		this.isSubstance = getLookAndFeel().getName().equalsIgnoreCase(
99 				"Substance");
100 	}
101 
102 	@Override
getComponent(final JComponent superComponent, final JTable t, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column)103 	public JComponent getComponent(final JComponent superComponent,
104 			final JTable t, final Object value, final boolean isSelected,
105 			final boolean hasFocus, final int row, final int column) {
106 		// Get result from super renderer
107 		JComponent c = this.renderer.getComponent(superComponent, t, value,
108 				isSelected, hasFocus, row, column);
109 
110 		// Apply component orientation
111 		IColumnSet columnSet = this.navigatorColumnSet;
112 		if (!this.navigationHandler.getCurrentView()
113 				.isUseDefaultNavigatorColumnSet()) {
114 			columnSet = this.navigationHandler.getCurrentView()
115 					.getCustomColumnSet();
116 		}
117 
118 		((JLabel) c).setHorizontalAlignment(columnSet.getColumn(
119 				columnSet.getColumnId(column)).getAlignment());
120 
121 		// Check incomplete tags if necessary
122 		if (this.stateNavigation.isHighlightIncompleteTagElements()) {
123 			IAudioObject audioObject = this.navigationHandler
124 					.getAudioObjectInNavigationTable(row);
125 			if (this.tagHandler.hasIncompleteTags(audioObject)) {
126 				((JLabel) c).setForeground(Color.red);
127 			} else {
128 				// Only Substance doesn't need this workaround
129 				if (!this.isSubstance) {
130 					((JLabel) c).setForeground(c.getForeground());
131 					if (isSelected) {
132 						((JLabel) c).setForeground(UIManager
133 								.getColor("List.selectionForeground"));
134 					} else {
135 						((JLabel) c).setForeground(UIManager
136 								.getColor("List.foreground"));
137 					}
138 				}
139 			}
140 		}
141 		return c;
142 	}
143 }