Skip to content

Commit

Permalink
Fixed issue #227.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsschmidt1337 committed Apr 2, 2016
1 parent 1871862 commit 4f6bf4c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 86 deletions.
98 changes: 14 additions & 84 deletions src/org/nschmidt/ldparteditor/data/ColourChanger.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
package org.nschmidt.ldparteditor.data;

import org.eclipse.swt.custom.StyledText;
import org.nschmidt.ldparteditor.helpers.math.MathHelper;
import org.nschmidt.ldparteditor.text.StringHelper;
import org.eclipse.swt.custom.CTabItem;
import org.nschmidt.ldparteditor.composites.compositetab.CompositeTab;
import org.nschmidt.ldparteditor.helpers.compositetext.Text2SelectionConverter;
import org.nschmidt.ldparteditor.project.Project;
import org.nschmidt.ldparteditor.shells.editortext.EditorTextWindow;

/**
* Sorts selected lines by a given criteria
Expand All @@ -28,90 +30,18 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
public enum ColourChanger {
INSTANCE;

public static void changeColour(StyledText st, int fromLine, int toLine, DatFile fileNameObj, int colourNumber, float r, float g, float b) {

// Backup selection range
final int x = st.getSelectionRange().x;
final int y = st.getSelectionRange().y;

// Replace colours

StringBuilder colourBuilder = new StringBuilder();
if (colourNumber == -1) {
colourBuilder.append("0x2"); //$NON-NLS-1$
colourBuilder.append(MathHelper.toHex((int) (255f * r)).toUpperCase());
colourBuilder.append(MathHelper.toHex((int) (255f * g)).toUpperCase());
colourBuilder.append(MathHelper.toHex((int) (255f * b)).toUpperCase());
} else {
colourBuilder.append(colourNumber);
}
String col = colourBuilder.toString();
StringBuilder newDatText = new StringBuilder();
final String ld = StringHelper.getLineDelimiter();
GData data2draw = fileNameObj.getDrawChainStart();
int lineCount = 0;
while ((data2draw = data2draw.getNext()) != null) {
lineCount += 1;
if (lineCount >= fromLine && lineCount <= toLine) {
switch (data2draw.type()) {
case 1:
{
GData1 gd = (GData1) data2draw;
newDatText.append(gd.colourReplace(col));
}
break;
case 2:
{
GData2 gd = (GData2) data2draw;
newDatText.append(gd.colourReplace(col));
}
break;
case 3:
{
GData3 gd = (GData3) data2draw;
newDatText.append(gd.colourReplace(col));
}
break;
case 4:
{
GData4 gd = (GData4) data2draw;
newDatText.append(gd.colourReplace(col));
}
break;
case 5:
{
GData5 gd = (GData5) data2draw;
newDatText.append(gd.colourReplace(col));
}
break;
default:
newDatText.append(data2draw.toString());
public static void changeColour(int lineStart, int lineEnd, DatFile datFile, int colourNumber, float r, float g, float b, float a) {
Text2SelectionConverter.convert(lineStart, lineEnd, datFile);
datFile.getVertexManager().skipSyncTimer();
datFile.getVertexManager().colourChangeSelection(colourNumber, r, g, b, a);
for (EditorTextWindow w : Project.getOpenTextWindows()) {
for (CTabItem t : w.getTabFolder().getItems()) {
if (datFile.equals(((CompositeTab) t).getState().getFileNameObj())) {
((CompositeTab) t).parseForErrorAndHints();
((CompositeTab) t).getTextComposite().redraw();
break;
}
} else {
newDatText.append(data2draw.toString());
}

if (data2draw.getNext() != null) newDatText.append(ld);
}

st.setText(newDatText.toString());


// Restore selection range

try {
st.setSelectionRange(x, y);
} catch (IllegalArgumentException iae1) {
try {
st.setSelectionRange(x - 1, y);
} catch (IllegalArgumentException iae2) {
try {
st.setSelectionRange(x - 2, y);
} catch (IllegalArgumentException consumed) {}
}
}

st.showSelection();
}
}
9 changes: 9 additions & 0 deletions src/org/nschmidt/ldparteditor/data/VM00Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public final ThreadsafeHashMap<GData, Set<VertexInfo>> getLineLinkedToVertices()
protected final HashMap<GData, Byte> bfcMap = new HashMap<GData, Byte>();

protected volatile AtomicBoolean resetTimer = new AtomicBoolean(false);
protected volatile AtomicBoolean skipTimer = new AtomicBoolean(false);
protected volatile AtomicInteger tid = new AtomicInteger(0);
protected volatile AtomicInteger openThreads = new AtomicInteger(0);
protected volatile Lock lock = new ReentrantLock();
Expand Down Expand Up @@ -214,6 +215,9 @@ public final void syncWithTextEditors(boolean addHistory) {
public void run() {
openThreads.incrementAndGet();
do {
if (skipTimer.get()) {
break;
}
resetTimer.set(false);
for(int i = 0; i < 4; i++) {
try {
Expand All @@ -223,6 +227,7 @@ public void run() {
if (tid2.get() != tid.get()) break;
}
} while (resetTimer.get());
skipTimer.set(false);
openThreads.decrementAndGet();
if (tid2.get() != tid.get() || isSkipSyncWithTextEditor() || !isSyncWithTextEditor()) return;
boolean notFound = true;
Expand Down Expand Up @@ -1798,4 +1803,8 @@ public final synchronized void clear() {
selectedCondlines.clear();
lastSelectedVertex = null;
}

public void skipSyncTimer() {
skipTimer.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum BFCswapper {
public static void swap(int lineStart, int lineEnd, DatFile datFile) {
Text2SelectionConverter.convert(lineStart, lineEnd, datFile);
datFile.getVertexManager().backupHideShowState();
datFile.getVertexManager().skipSyncTimer();
datFile.getVertexManager().windingChangeSelection();
for (EditorTextWindow w : Project.getOpenTextWindows()) {
for (CTabItem t : w.getTabFolder().getItems()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public void widgetSelected(SelectionEvent e) {
toLine++;
NLogger.debug(getClass(), "From line {0}", fromLine); //$NON-NLS-1$
NLogger.debug(getClass(), "To line {0}", toLine); //$NON-NLS-1$
ColourChanger.changeColour(st, fromLine, toLine, df, num, gColour2[0].getR(), gColour2[0].getG(), gColour2[0].getB());
ColourChanger.changeColour(fromLine, toLine, df, num, gColour2[0].getR(), gColour2[0].getG(), gColour2[0].getB(), gColour2[0].getA());
st.forceFocus();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ public void widgetSelected(SelectionEvent e) {
toLine++;
NLogger.debug(getClass(), "From line {0}", fromLine); //$NON-NLS-1$
NLogger.debug(getClass(), "To line {0}", toLine); //$NON-NLS-1$
ColourChanger.changeColour(st, fromLine, toLine, df, num, gColour2[0].getR(), gColour2[0].getG(), gColour2[0].getB());
ColourChanger.changeColour(fromLine, toLine, df, num, gColour2[0].getR(), gColour2[0].getG(), gColour2[0].getB(), gColour2[0].getA());
st.forceFocus();
}
}
Expand Down

0 comments on commit 4f6bf4c

Please sign in to comment.