Richard S. Hall | 5c23fb9 | 2006-09-28 19:55:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
Richard S. Hall | 54d1e2e | 2006-04-05 14:19:38 +0000 | [diff] [blame] | 9 | * |
Richard S. Hall | 5c23fb9 | 2006-09-28 19:55:37 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Richard S. Hall | 54d1e2e | 2006-04-05 14:19:38 +0000 | [diff] [blame] | 11 | * |
Richard S. Hall | 5c23fb9 | 2006-09-28 19:55:37 +0000 | [diff] [blame] | 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
Richard S. Hall | 54d1e2e | 2006-04-05 14:19:38 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.apache.felix.shell.gui.plugin; |
| 20 | |
| 21 | import java.awt.BorderLayout; |
| 22 | import java.awt.Component; |
| 23 | import java.awt.event.KeyAdapter; |
| 24 | import java.awt.event.KeyEvent; |
| 25 | import java.io.PrintStream; |
| 26 | |
| 27 | import javax.swing.JPanel; |
| 28 | import javax.swing.JTextField; |
| 29 | |
| 30 | import org.apache.felix.shell.ShellService; |
| 31 | import org.apache.felix.shell.gui.Plugin; |
| 32 | import org.osgi.framework.BundleContext; |
| 33 | import org.osgi.framework.ServiceReference; |
| 34 | |
| 35 | public class ShellPlugin extends JPanel implements Plugin |
| 36 | { |
| 37 | private BundleContext m_context = null; |
| 38 | private PrintStream m_out = null; |
| 39 | |
| 40 | private JTextField m_commandField = new JTextField(); |
| 41 | private String[] m_history = new String[25]; |
| 42 | private int m_historyCount = -1; |
| 43 | private int m_current = 0; // current history counter |
| 44 | |
| 45 | private ScrollableOutputArea m_soa = new ScrollableOutputArea(); |
| 46 | |
| 47 | // Plugin interface methods. |
| 48 | |
| 49 | public String getName() |
| 50 | { |
| 51 | return "Shell"; |
| 52 | } |
| 53 | |
| 54 | public Component getGUI() |
| 55 | { |
| 56 | return this; |
| 57 | } |
| 58 | |
| 59 | // Implementation. |
| 60 | |
| 61 | public ShellPlugin(BundleContext context) |
| 62 | { |
| 63 | m_context = context; |
| 64 | m_out = new PrintStream(new OutputAreaStream(m_soa)); |
| 65 | initialize(); |
| 66 | } |
| 67 | |
| 68 | private void initialize() |
| 69 | { |
| 70 | setLayout(new BorderLayout()); |
| 71 | m_commandField.addKeyListener(new KeyAdapter() { |
| 72 | public void keyPressed(KeyEvent event) |
| 73 | { |
| 74 | commandFieldKeyPressed(event); |
| 75 | } |
| 76 | }); |
| 77 | m_commandField.setFont(new java.awt.Font("Monospaced", 0, 12)); |
| 78 | add(m_commandField, BorderLayout.SOUTH); |
| 79 | add(m_soa, BorderLayout.CENTER); |
| 80 | } |
| 81 | |
| 82 | void processCommand(String line) |
| 83 | { |
| 84 | if (line == null) |
| 85 | { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | line = line.trim(); |
| 90 | |
| 91 | if (line.length() == 0) |
| 92 | { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // Get shell service. |
| 97 | ServiceReference ref = m_context.getServiceReference( |
| 98 | org.apache.felix.shell.ShellService.class.getName()); |
| 99 | if (ref == null) |
| 100 | { |
| 101 | m_out.println("No shell service is available."); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | ShellService shell = (ShellService) m_context.getService(ref); |
| 106 | |
| 107 | // Print the command line in the output window. |
| 108 | m_out.println("-> " + line); |
| 109 | |
| 110 | try { |
| 111 | shell.executeCommand(line, m_out, m_out); |
| 112 | } catch (Exception ex) { |
| 113 | m_out.println(ex.toString()); |
| 114 | ex.printStackTrace(m_out); |
| 115 | } |
| 116 | |
| 117 | m_context.ungetService(ref); |
| 118 | } |
| 119 | |
| 120 | private void addToHistory(String command) |
| 121 | { |
| 122 | m_historyCount++; |
| 123 | if (m_historyCount >= m_history.length) |
| 124 | { |
| 125 | m_historyCount = m_history.length - 1; |
| 126 | for (int i = 0; i < m_history.length - 1; i++) |
| 127 | { |
| 128 | m_history[i] = m_history[i + 1]; |
| 129 | } |
| 130 | } |
| 131 | m_history[m_historyCount] = new String(command); |
| 132 | } |
| 133 | |
| 134 | private String getFromHistory(int num) |
| 135 | { |
| 136 | if (num < 0) |
| 137 | { |
| 138 | return (""); |
| 139 | } |
| 140 | else if (num > m_historyCount) |
| 141 | { |
| 142 | return(""); |
| 143 | } |
| 144 | return m_history[num]; |
| 145 | } |
| 146 | |
| 147 | protected void commandFieldKeyPressed(KeyEvent event) |
| 148 | { |
| 149 | String command = null; |
| 150 | int c = event.getKeyCode(); |
| 151 | // Cursor up. |
| 152 | if (c == 38) |
| 153 | { |
| 154 | m_current--; |
| 155 | if (m_current < 0) |
| 156 | { |
| 157 | m_current = 0; |
| 158 | } |
| 159 | m_commandField.setText(getFromHistory(m_current)); |
| 160 | m_commandField.setCaretPosition(m_commandField.getText().length()); |
| 161 | } |
| 162 | // Cursor down. |
| 163 | else if (c == 40) |
| 164 | { |
| 165 | m_current++; |
| 166 | if (m_current > m_historyCount) |
| 167 | { |
| 168 | m_current = m_historyCount + 1; |
| 169 | } |
| 170 | m_commandField.setText(getFromHistory(m_current)); |
| 171 | m_commandField.setCaretPosition(m_commandField.getText().length()); |
| 172 | } |
| 173 | else if (c == 10) |
| 174 | { |
| 175 | command = m_commandField.getText(); |
| 176 | if (!command.equals("")) |
| 177 | { |
| 178 | addToHistory(command); |
| 179 | m_current = m_historyCount + 1; |
| 180 | processCommand(command); |
| 181 | } |
| 182 | m_commandField.setText(""); |
| 183 | } |
| 184 | } |
| 185 | } |