Skip to content

Commit

Permalink
Merge pull request #736 from csmith/master
Browse files Browse the repository at this point in the history
Add an interface for ColourManager.
  • Loading branch information
greboid committed Dec 30, 2016
2 parents 182a6ee + 82f897f commit 646b305
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 37 deletions.
41 changes: 41 additions & 0 deletions api/src/main/java/com/dmdirc/ui/messages/ColourManager.java
@@ -0,0 +1,41 @@
package com.dmdirc.ui.messages;

import com.dmdirc.util.colours.Colour;

/**
* Created by Chris on 30/12/2016.
*/
public interface ColourManager {
/**
* Parses either a 1-2 digit IRC colour, or a 6 digit hex colour from the target string, and
* returns the corresponding colour. Returns the specified fallback colour if the spec can't be
* parsed.
*
* @param spec The string to parse
* @param fallback The colour to use if the spec isn't valid
*
* @return A colour representation of the specified string
*/
Colour getColourFromString(String spec, Colour fallback);

/**
* Returns a Colour object that corresponds to the specified 6-digit hex string. If the string
* is invalid, logs a warning and returns white.
*
* @param hex The hex string to convert into a Colour
*
* @return A Colour object corresponding to the hex input
*/
Colour getColourFromHex(String hex);

/**
* Returns a Colour object that represents the colour associated with the specified IRC colour
* code. If the code is not found, a warning is logged with the client's Logger class, and white
* is returned.
*
* @param number The IRC colour code to look up
*
* @return The corresponding Colour object
*/
Colour getColourFromIrcCode(int number);
}
Expand Up @@ -38,7 +38,7 @@ public ColourManagerFactory() {
}

public ColourManager getColourManager(final AggregateConfigProvider configManager) {
return new ColourManager(configManager);
return new ColourManagerImpl(configManager);
}

}
Expand Up @@ -39,9 +39,9 @@
* The colour manager manages the colour scheme for the IRC client. It allows other components to
* use IRC colour codes instead of absolute colours.
*/
public class ColourManager {
public class ColourManagerImpl implements ColourManager {

private static final Logger LOG = LoggerFactory.getLogger(ColourManager.class);
private static final Logger LOG = LoggerFactory.getLogger(ColourManagerImpl.class);
/** Default colours used for the standard 16 IRC colours. */
private static final Colour[] DEFAULT_COLOURS = {
Colour.WHITE, Colour.BLACK, new Colour(0, 0, 127), new Colour(0, 141, 0),
Expand All @@ -56,11 +56,11 @@ Colour.YELLOW, new Colour(0, 252, 0), new Colour(0, 128, 128), new Colour(0, 255
private final Colour[] ircColours = DEFAULT_COLOURS.clone();

/**
* Creates a new instance of {@link ColourManager}.
* Creates a new instance of {@link ColourManagerImpl}.
*
* @param configManager The manager to read config settings from.
*/
public ColourManager(final AggregateConfigProvider configManager) {
public ColourManagerImpl(final AggregateConfigProvider configManager) {
this.configManager = configManager;

configManager.addChangeListener("colour", (domain, key) -> initColours());
Expand All @@ -85,16 +85,7 @@ private void initColours() {
}
}

/**
* Parses either a 1-2 digit IRC colour, or a 6 digit hex colour from the target string, and
* returns the corresponding colour. Returns the specified fallback colour if the spec can't be
* parsed.
*
* @param spec The string to parse
* @param fallback The colour to use if the spec isn't valid
*
* @return A colour representation of the specified string
*/
@Override
public Colour getColourFromString(final String spec, final Colour fallback) {
if (colourCache.containsKey(spec)) {
return colourCache.get(spec);
Expand Down Expand Up @@ -130,14 +121,7 @@ public Colour getColourFromString(final String spec, final Colour fallback) {
return res;
}

/**
* Returns a Colour object that corresponds to the specified 6-digit hex string. If the string
* is invalid, logs a warning and returns white.
*
* @param hex The hex string to convert into a Colour
*
* @return A Colour object corresponding to the hex input
*/
@Override
public Colour getColourFromHex(final String hex) {
if (colourCache.containsKey(hex)) {
return colourCache.get(hex);
Expand All @@ -163,15 +147,7 @@ public Colour getColourFromHex(final String hex) {
return colour;
}

/**
* Returns a Colour object that represents the colour associated with the specified IRC colour
* code. If the code is not found, a warning is logged with the client's Logger class, and white
* is returned.
*
* @param number The IRC colour code to look up
*
* @return The corresponding Colour object
*/
@Override
public Colour getColourFromIrcCode(final int number) {
if (number >= 0 && number <= 15) {
return ircColours[number];
Expand Down
Expand Up @@ -57,7 +57,7 @@ public class UnreadStatusManagerImpl implements UnreadStatusManager {
public UnreadStatusManagerImpl(final WindowModel container) {
this.container = container;
this.eventBus = container.getEventBus();
this.colourManager = new ColourManager(container.getConfigManager());
this.colourManager = new ColourManagerImpl(container.getConfigManager());
}

@Handler
Expand Down
Expand Up @@ -57,7 +57,7 @@ public class ColourManagerTest {

@Before
public void setup() {
manager = new ColourManager(configManager);
manager = new ColourManagerImpl(configManager);
verify(configManager).addChangeListener(anyString(), configListener.capture());
}

Expand Down
Expand Up @@ -55,7 +55,7 @@ public IntelligentLinkingTest(final String input, final String expected) {
when(connection.getGroupChatManager()).thenReturn(groupChatManager);
when(groupChatManager.getChannelPrefixes()).thenReturn("#&+");

styliser = new Styliser(connection, manager, new ColourManager(manager));
styliser = new Styliser(connection, manager, new ColourManagerImpl(manager));
}

@Test
Expand Down
Expand Up @@ -69,7 +69,7 @@ protected static String style(final String input, final EventBus eventBus)

final AggregateConfigProvider manager = mock(AggregateConfigProvider.class);

final Styliser styliser = new Styliser(null, manager, new ColourManager(manager));
final Styliser styliser = new Styliser(null, manager, new ColourManagerImpl(manager));
styliser.addStyledString(null, input); // TODO...
final AttributedCharacterIterator aci = null; // TODO...

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/dmdirc/ui/messages/StyliserTest.java
Expand Up @@ -70,7 +70,7 @@ public void testNegation() {
final String input2 = "abcdefghi";

final AggregateConfigProvider manager = mock(AggregateConfigProvider.class);
final Styliser styliser = new Styliser(null, manager, new ColourManager(manager));
final Styliser styliser = new Styliser(null, manager, new ColourManagerImpl(manager));

for (int i = 0; i < input2.length(); i++) {
// TODO...
Expand Down

0 comments on commit 646b305

Please sign in to comment.