Skip to content

Commit

Permalink
issue 47 - Ability to import existing .ino or .c GUIslice file
Browse files Browse the repository at this point in the history
  • Loading branch information
Pconti31 committed Jan 27, 2019
1 parent 349c57f commit b6a403c
Show file tree
Hide file tree
Showing 33 changed files with 5,608 additions and 160 deletions.
143 changes: 143 additions & 0 deletions builder/src/main/java/builder/common/ColorImport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
*
* The MIT License
*
* Copyright 2019 Paul Conti
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package builder.common;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* A mapping class for importing GUIslice Library Color objects.
* It's bascally the inverse of our ColorFactory class.
*
* @author Paul Conti
*
*/
public class ColorImport {

/** The Constant DEFAULT_COLORS_FILE. */
public static final String DEFAULT_COLORS_FILE = "default_colors.csv";

/** The instance. */
private static ColorImport instance = null;

/** The colors list. */
private static List<ColorItem> colorsList = new ArrayList<ColorItem>();

/**
* Gets the single instance of ColorImport.
*
* @return single instance of ColorImport
*/
public static synchronized ColorImport getInstance() {
if (instance == null) {
instance = new ColorImport();
String fullPath;
String strUserDir = System.getProperty("user.dir");
int n = strUserDir.indexOf("bin");
if (n > 0) {
strUserDir = strUserDir.substring(0,n-1); // remove "/bin"
}
fullPath = strUserDir + System.getProperty("file.separator") + "templates"
+ System.getProperty("file.separator");
String csvFile = fullPath + DEFAULT_COLORS_FILE;
instance.readDefaultColors(csvFile);
}
return instance;
}

/**
* Instantiates a new color import instance.
*/
public ColorImport() {
}

/**
* Gets the color list.
*
* @return the color list
*/
public List<ColorItem> getColorList() {
return colorsList;
}

/**
* colorAsString() - convert java Color object to a string GUIslice API can use
* Example: We will return either a #define like "Color.BLACK" or if no matching
* #define can be found we will return the red, green, blue as
* "(gslc_tsColor){999,999,999}"
*
* @param color
* the color
* @return GUIslice Library string representing the color
*/
public String colorAsString(Color color) {
String strColor = "";
for (int i=1; i<colorsList.size(); i++) {
ColorItem item = colorsList.get(i);
if (item.getColor().equals(color)) {
return item.getDisplayName();
}
}
strColor = String.format("(gslc_tsColor){%d,%d,%d}", color.getRed(), color.getGreen(), color.getBlue());
return strColor;
}

/**
* Read GUIslice api colors.
*
* @param csvFile
* the csv file
*/
private void readDefaultColors(String csvFile) {
String line = "";
String cvsSplitBy = ",";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(csvFile));
int i =0;
while ((line = br.readLine()) != null) {
// Need to skip comment lines
if (!line.startsWith("#")) {
String[] f = line.split(cvsSplitBy);
// the first seven non-comment line are the default colors and are not needed for imports
if (i>6) {
ColorItem item = new ColorItem(i-7, f[0], Integer.parseInt(f[1]), Integer.parseInt(f[2]), Integer.parseInt(f[3]));
colorsList.add(item);
}
i++;
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
16 changes: 6 additions & 10 deletions builder/src/main/java/builder/common/EnumFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ public class EnumFactory {
/** The count keys. */
private static int[] countKeys;

/** The count enums. */
private static int[] countEnums;

/** The Constant numberOfTypes. */
static final public int numberOfTypes = 16;

Expand Down Expand Up @@ -151,7 +148,6 @@ public static synchronized EnumFactory getInstance() {
*/
public EnumFactory() {
countKeys = new int[EnumFactory.numberOfTypes];
countEnums = new int[EnumFactory.numberOfTypes];
clearCounts();
}

Expand Down Expand Up @@ -204,10 +200,9 @@ public String createEnum(String type) {
break;
}
}
countEnums[i]++;
if (type == PAGE && countEnums[i] == 1)
if (type == PAGE && countKeys[i] == 1)
return EnumFactory.PAGE_MAIN;
return String.format("%s%d", strEnum,countEnums[i]);
return String.format("%s%d", strEnum,countKeys[i]);
}

/**
Expand All @@ -216,7 +211,6 @@ public String createEnum(String type) {
public void clearCounts() {
for (int i=0; i<(EnumFactory.numberOfTypes); i++) {
countKeys[i]= 0;
countEnums[i]= 0;
}
}

Expand All @@ -234,7 +228,7 @@ public String backup() {
out.writeInt(nodeCount);
for(int i=0; i<nodeCount; i++) {
out.writeInt(countKeys[i]);
out.writeInt(countEnums[i]);
out.writeInt(countKeys[i]);
}
out.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
Expand All @@ -255,12 +249,14 @@ public String backup() {
public void restore(String state) {
try {
// System.out.println("enum restore====");
@SuppressWarnings("unused")
int skip=0;
byte[] data = Base64.getDecoder().decode(state);
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));
int nodeCount = in.readInt();
for (int i=0; i<nodeCount; i++) {
countKeys[i] = in.readInt();
countEnums[i] = in.readInt();
skip = in.readInt();
}
in.close();
} catch (IOException e) {
Expand Down
73 changes: 71 additions & 2 deletions builder/src/main/java/builder/common/FontFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,36 @@ public Font getFont(String key) {
* @return the font name
*/
public String getDefFontName() {
FontItem item = null;
String target = Builder.testPlatform;
if (target == null) {
target = generalModel.getTarget();
}
if (target.equals("linux")) {
item = linuxFonts.get(0);
} else {
item = arduinoFonts.get(0);
}
return item.getDisplayName();
}

/**
* Gets the name of the default font enum for the target platform.
*
* @return the font enum
*/
public String getDefFontEnum() {
FontItem item = null;
String target = Builder.testPlatform;
if (target == null) {
target = generalModel.getTarget();
}
if (target.equals("linux")) {
return linuxFonts.get(0).getDisplayName();
item = linuxFonts.get(0);
} else {
return arduinoFonts.get(0).getDisplayName();
item = arduinoFonts.get(0);
}
return item.getFontId();
}

/**
Expand All @@ -210,6 +231,54 @@ public List<FontItem> getFontList() {
}
}

/**
* Gets the font enum.
*
* @param key
* the key (display name)
* @return the font enum
*/
public String getFontEnum(String key) {
FontItem item = null;
Integer idx = Integer.valueOf(0); // always return something...
String target = Builder.testPlatform;
if (target == null) {
target = generalModel.getTarget();
}
if (target.equals("linux")) {
if (linuxMap.containsKey(key)) {
idx = linuxMap.get(key);
}
item = linuxFonts.get(idx.intValue());
} else {
if (arduinoMap.containsKey(key)) {
idx = arduinoMap.get(key);
}
item = arduinoFonts.get(idx.intValue());
}
return item.getFontId();
}

/**
* Gets the font display name.
*
* @param key
* the key (font enum)
* @return the font display name or null on failure
*/
public String getFontDisplayName(String key) {
String name = null;
List<FontItem> list = getFontList();
// this isn't called often enough to warrant anything but brute force search
for (FontItem item : list) {
if (item.getFontId().equals(key)) {
name = item.getDisplayName();
break;
}
}
return name;
}

/**
* Gets the font item.
*
Expand Down
Loading

0 comments on commit b6a403c

Please sign in to comment.