Skip to content

Commit d18f2f1

Browse files
committed
Merge pull request #31 from eprogramming/master
this close #4
2 parents 3b27ecc + 49631d4 commit d18f2f1

File tree

4 files changed

+69
-64
lines changed

4 files changed

+69
-64
lines changed

src/main/java/org/esmerilprogramming/mterm/event/BlockAction.java renamed to src/main/java/org/esmerilprogramming/mterm/event/BlockPromptStringDeletionAction.java

+18-17
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,30 @@
1616
import java.awt.event.ActionEvent;
1717

1818
import javax.swing.AbstractAction;
19+
import javax.swing.Action;
1920
import javax.swing.JTextArea;
2021

21-
import org.esmerilprogramming.mterm.handler.AeshHandler;
22-
2322
/**
24-
* BlockAction class.
25-
*
23+
* BlockPromptStringDeletionAction class.
24+
* This class block the deletion of the PS1.
25+
*
2626
* @author <a href="mailto:[email protected]">Helio Frota</a>
2727
*/
2828
@SuppressWarnings("serial")
29-
public class BlockAction extends AbstractAction {
30-
31-
private AeshHandler aesh;
32-
private JTextArea textArea;
33-
34-
public BlockAction(JTextArea textArea, AeshHandler aesh) {
35-
this.textArea = textArea;
36-
this.aesh = aesh;
29+
public class BlockPromptStringDeletionAction extends AbstractAction {
30+
31+
private int promptStringLength;
32+
private Action blockPromptStringDeletion;
33+
34+
public BlockPromptStringDeletionAction(int promptStringLength, Action blockPromptStringDeletion) {
35+
this.promptStringLength = promptStringLength;
36+
this.blockPromptStringDeletion = blockPromptStringDeletion;
3737
}
38-
39-
public void actionPerformed(ActionEvent ev) {
40-
38+
39+
public void actionPerformed(ActionEvent e) {
40+
JTextArea textarea = (JTextArea) e.getSource();
41+
if (textarea.getCaretPosition() > promptStringLength) {
42+
blockPromptStringDeletion.actionPerformed(null);
43+
}
4144
}
42-
43-
4445
}

src/main/java/org/esmerilprogramming/mterm/event/MtermDocListener.java

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2014 EsmerilProgramming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package org.esmerilprogramming.mterm.event;
15+
16+
import javax.swing.Action;
17+
import javax.swing.JTextArea;
18+
import javax.swing.text.NavigationFilter;
19+
import javax.swing.text.Position;
20+
21+
/**
22+
* MtermNavigationFilter class.
23+
* This class take care of the caret position.
24+
*
25+
* @author <a href="mailto:[email protected]">Helio Frota</a>
26+
*/
27+
public class MtermNavigationFilter extends NavigationFilter {
28+
private int promptStringLength;
29+
private Action blockPromptStringDeletionAction;
30+
31+
public MtermNavigationFilter(int promptStringLength, JTextArea textarea) {
32+
this.promptStringLength = promptStringLength;
33+
blockPromptStringDeletionAction = textarea.getActionMap().get("delete-previous");
34+
textarea.getActionMap().put("delete-previous",
35+
new BlockPromptStringDeletionAction(promptStringLength, blockPromptStringDeletionAction));
36+
textarea.setCaretPosition(promptStringLength);
37+
}
38+
39+
public void setDot(NavigationFilter.FilterBypass filter, int dot, Position.Bias bias) {
40+
filter.setDot(Math.max(dot, promptStringLength), bias);
41+
}
42+
43+
public void moveDot(NavigationFilter.FilterBypass filter, int dot, Position.Bias bias) {
44+
filter.moveDot(Math.max(dot, promptStringLength), bias);
45+
}
46+
47+
48+
}

src/main/java/org/esmerilprogramming/mterm/gui/MtermUI.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import javax.swing.KeyStroke;
3333

3434
import org.esmerilprogramming.mterm.Mterm;
35-
import org.esmerilprogramming.mterm.event.MtermDocListener;
35+
import org.esmerilprogramming.mterm.event.MtermNavigationFilter;
3636
import org.esmerilprogramming.mterm.event.RunAction;
3737
import org.esmerilprogramming.mterm.event.TabAction;
3838
import org.esmerilprogramming.mterm.handler.AeshHandler;
@@ -53,7 +53,6 @@ public final class MtermUI extends JFrame {
5353
public MtermUI() {
5454
initGraphComponents();
5555
applyEventsAndStreams();
56-
System.out.print(Mterm.buildPS1());
5756
}
5857

5958
private void initGraphComponents() {
@@ -79,7 +78,7 @@ private void initGraphComponents() {
7978

8079
setJMenuBar(m.create());
8180

82-
textArea = new JTextArea(21, 80);
81+
textArea = new JTextArea(Mterm.buildPS1(), 21, 80);
8382
textArea.setFont(new Font("Monospaced", Font.PLAIN, 15));
8483
textArea.setLineWrap(true);
8584
textArea.setWrapStyleWord(true);
@@ -135,9 +134,7 @@ public void actionPerformed(ActionEvent arg0) {
135134
textArea.getActionMap().put("tab", new TabAction(textArea, aesh));
136135
textArea.getInputMap().put(KeyStroke.getKeyStroke("UP"), "none");
137136
textArea.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "none");
138-
textArea.getInputMap().put(KeyStroke.getKeyStroke("BACK_SPACE"), "none");
139-
140-
textArea.getDocument().addDocumentListener(new MtermDocListener());
137+
textArea.setNavigationFilter(new MtermNavigationFilter(Mterm.buildPS1().length(), textArea));
141138
}
142139

143140
public JTextArea getTextArea() {

0 commit comments

Comments
 (0)